Vize

script/prefer-use-id

Recommend using useId() for generating unique IDs (Vue 3.5+)

Bad · Good

Default severity: warning
Presets: none
Automatic fix: None; review the suggested change
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form
Options: No rule-specific options. Severity and preset selection are configurable.

Configuration (Vite+)

import { defineConfig } from "@vizejs/vite-plugin/vite-plus";

export default defineConfig({
  lint: {
    vize: {
      "preset": "incremental",
      "rules": {
        "script/prefer-use-id": "warn"
      }
    },
  },
});
vp run lint

Bad

id contains Math.random(), so the generated input/label identifier can differ between server and client rendering. Its ID-named binding is the rule’s recognized generation context.

<script setup lang="ts">
const id = `input-${Math.random()}`;
</script>
<template><label :for="id">Name</label><input :id="id" /></template>

Good

Vue 3.5+ useId() generates the identifier, and both :for and :id continue reading the same binding instead of independently generating random values.

<script setup lang="ts">
import { useId } from "vue";
const id = useId();
</script>
<template><label :for="id">Name</label><input :id="id" /></template>

Good avoids this rule's finding under the configuration above; other rules may still report diagnostics.

Implementation · All rules