Rules
no-implicit-key
Full Name in @eslint-react/eslint-plugin
@eslint-react/no-implicit-keyFull Name in eslint-plugin-react-x
react-x/no-implicit-keyFeatures
๐ญ ๐งช
Presets
recommended-type-checked
strict-type-checked
Description
Prevents implicitly passing the 'key' prop to components.
This makes it hard to see whether the key was passed correctly to the element or where it came from.
It is also proposed to be deprecated in this RFC: Deprecate spreading key from objects.
Examples
Failing
import React from "react";
interface Foo = { key?: string; }
interface MyComponentProps extends Foo {
className: string;
children: React.ReactNode;
}
function MyComponent(props: MyComponentProps) {
return <div {...props} />;
// ^^^^^^^^^^
// - This spread attribute implicitly passes the 'key' prop to a component, this could lead to unexpected behavior. If you intend to pass the 'key' prop, use 'key={value}'.
}Passing
import React from "react";
interface Foo = { key?: string; }
interface MyComponentProps extends Foo {
className: string;
children: React.ReactNode;
}
function MyComponent({ key, ...rest }) {
return <div {...rest} />;
}import React from "react";
function PaginationItem({ ...props }: React.ComponentProps<"li">) {
return <li data-slot="pagination-item" {...props} />;
// ^^^^^^^^^^
// - Passing-through React internally defined keys are allowed.
}Implementation
See Also
jsx-key-before-spread
Enforces that thekeyattribute is placed before the spread attribute in JSX elements.no-missing-key
Prevents missingkeyon items in list rendering.no-duplicate-key
Prevents duplicatekeyprops on sibling elements when rendering lists.no-array-index-key
Warns when an arrayindexis used as akeyprop.