src/content/docs/linter/rules/use-unique-element-ids.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/correctness/useUniqueElementIds`](/reference/diagnostics#diagnostic-category) - 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**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`react`](/linter/domains#react) ## How to configure ```json title="biome.json" { "linter": { "rules": { "correctness": { "useUniqueElementIds": "error" } } } }## Description
Prevent the usage of static string literal `id` attribute on elements.
In React, hardcoding IDs is discouraged because IDs have to be unique in the DOM.
You should use [`useId`](https://react.dev/reference/react/useId) to generate unique IDs for accessibility purposes.
Please keep in mind this rule doesn't check whether ids are actually unique or not, and does check whether static literal id isn't passed to the elements or not. So you're encouraged to check by yourself if the ids are actually unique.
## Examples
### Invalid
```jsx
<div id="foo">bar</div>;
React.createElement("div", { id: "foo" });
const id = useId();
<div id={id}>bar</div>;
const id = useId();
React.createElement("div", { id });
The following option is available
excludedComponentsList 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": {
"options": {
"excludedComponents": [
"FormattedMessage"
]
}
}
}
}
}
}
<FormattedMessage id="static" />
<Library.FormattedMessage id="static" />