Try @eslint-react/kit@beta โ†’
logoESLint React

no-implicit-key

Prevents implicitly passing the 'key' prop to components.

This rule is experimental and may change in the future or be removed. It is not recommended for use in production code at this time.

Full Name in eslint-plugin-react-x

react-x/no-implicit-key

Full Name in @eslint-react/eslint-plugin

@eslint-react/no-implicit-key

Features

๐Ÿ’ญ ๐Ÿงช

Rule Details

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.

The following cases are allowed and will not be reported:

  1. The key property originates from React's own type definitions (ex: React.Attributes.key), such as when spreading React.ComponentProps<"div">, React.HTMLAttributes<T>, or similar React-provided types.
  2. The key property's type is React.Key, even if the property is declared in a user-defined type.

Examples

Implicitly passing key via the spread operator

Spreading an object that contains key directly onto a component makes the source and purpose of key unclear.

import React from "react";

declare let someValues: { foo: string; bar: string; key: string };

// Problem: Implicitly passing key when spreading an object
function MyComponent() {
  return <div {...someValues} />;
}
import React from "react";

declare let someValues: { id: string; className: string; key: string };

// Recommended: Explicitly destructure and pass key
function MyComponent() {
  const { key, ...rest } = someValues;
  return <div key={key} {...rest} />;
}

key type is React.Key

When the key type is the React built-in React.Key, spreading is allowed.

import React from "react";

declare let someValues: { id: string; className: string; key: React.Key };

// OK: key type is React.Key, which is a React built-in type
function MyComponent() {
  return <div {...someValues} data-slot="pagination-item" />;
}

Spreading React built-in component props

When spreading props from React built-in types (such as React.ComponentProps), the key originates from React's type definitions and is allowed.

import React from "react";

// OK: props come from React.ComponentProps, key originates from React.Attributes
function PaginationItem({ ...props }: React.ComponentProps<"li">) {
  return <li data-slot="pagination-item" {...props} />;
}

Versions

Resources

Further Reading


See Also

On this page