useUniqueElementIds (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.0.0 - Diagnostic Category:
lint/correctness/useUniqueElementIds - This rule isn’t recommended, so you need to enable it.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "useUniqueElementIds": "error" } } }}Description
Section titled “Description”Disallow static id values on elements outside SVG contexts.
IDs must be unique in the rendered page. A component with a hard-coded ID can create
duplicate IDs when it is rendered more than once, which can break links between labels and
controls or other accessibility features. Use React’s
useId to generate stable, unique IDs.
This rule reports static literal IDs. It does not compare IDs across elements or verify that dynamically generated IDs are unique.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<div id="foo">bar</div>;code-block.jsx:1:1 lint/correctness/useUniqueElementIds ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ id attribute should not be a static string literal. Generate unique IDs using useId().
> 1 │ <div id="foo">bar</div>;
│ ^^^^^^^^^^^^^^
2 │
ℹ In React, if you hardcode IDs and use the component multiple times, it can lead to duplicate IDs in the DOM. Instead, generate unique IDs using useId().
React.createElement("div", { id: "foo" });code-block.jsx:1:1 lint/correctness/useUniqueElementIds ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ id attribute should not be a static string literal. Generate unique IDs using useId().
> 1 │ React.createElement("div", { id: "foo" });
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ In React, if you hardcode IDs and use the component multiple times, it can lead to duplicate IDs in the DOM. Instead, generate unique IDs using useId().
const id = useId();<div id={id}>bar</div>;const id = useId();React.createElement("div", { id });Static IDs are allowed in SVG contexts:
<svg> <defs> <pattern id="dots" width="10" height="10" /> </defs> <rect fill="url(#dots)" width="100%" height="100%" /></svg>Options
Section titled “Options”The following option is available.
excludedComponents
Section titled “excludedComponents”List of unqualified component names to ignore.
Use it to list components expecting an id attribute that does not represent
a DOM element ID.
Default: empty list.
{ "linter": { "rules": { "correctness": { "useUniqueElementIds": { "level": "on", "options": { "excludedComponents": [ "FormattedMessage" ] } } } } }}<FormattedMessage id="static" /><Library.FormattedMessage id="static" />Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.