no-implicit-key (Deprecated)
Rule category
Suspicious.
What it does
Prevents key
prop from not being explicitly specified (e.g. spreading key
prop from objects).
Why is this bad?
This makes it hard to see if the key was passed correctly to the element or where it came from.
And it’s also be proposed to be deprecated is this RFC: Deprecate spreading key from objects
Examples
This rule aims to prevent spreading key from objects.
Failing
import React from "react";
interface ExampleProps {
ExampleProps.items: {
id: string;
name: string;
}[]
items: { id: string
id: string; name: string
name: string }[];
}
function function Example({ items }: ExampleProps): React.JSX.Element
Example({ items: {
id: string;
name: string;
}[]
items }: ExampleProps) {
return (
<JSX.IntrinsicElements.ul: React.DetailedHTMLProps<React.HTMLAttributes<HTMLUListElement>, HTMLUListElement>
ul>
{items: {
id: string;
name: string;
}[]
items.Array<{ id: string; name: string; }>.map<React.JSX.Element>(callbackfn: (value: {
id: string;
name: string;
}, index: number, array: {
id: string;
name: string;
}[]) => React.JSX.Element, thisArg?: any): React.JSX.Element[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.map((item: {
id: string;
name: string;
}
item) => (
<JSX.IntrinsicElements.li: React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>
li {...{ React.Attributes.key?: React.Key | null | undefined
key: item: {
id: string;
name: string;
}
item.id: string
id }}>{item: {
id: string;
name: string;
}
item.name: string
name}</JSX.IntrinsicElements.li: React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>
li>
// - Prefer specifying key explicitly instead of spreading it from object.
))}
</JSX.IntrinsicElements.ul: React.DetailedHTMLProps<React.HTMLAttributes<HTMLUListElement>, HTMLUListElement>
ul>
);
}
Passing
import React from "react";
interface ExampleProps {
ExampleProps.items: {
id: string;
name: string;
}[]
items: { id: string
id: string; name: string
name: string }[];
}
function function Example({ items }: ExampleProps): React.JSX.Element
Example({ items: {
id: string;
name: string;
}[]
items }: ExampleProps) {
return (
<JSX.IntrinsicElements.ul: React.DetailedHTMLProps<React.HTMLAttributes<HTMLUListElement>, HTMLUListElement>
ul>
{items: {
id: string;
name: string;
}[]
items.Array<{ id: string; name: string; }>.map<React.JSX.Element>(callbackfn: (value: {
id: string;
name: string;
}, index: number, array: {
id: string;
name: string;
}[]) => React.JSX.Element, thisArg?: any): React.JSX.Element[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.map((item: {
id: string;
name: string;
}
item) => (
<JSX.IntrinsicElements.li: React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>
li React.Attributes.key?: React.Key | null | undefined
key={item: {
id: string;
name: string;
}
item.id: string
id}>{item: {
id: string;
name: string;
}
item.name: string
name}</JSX.IntrinsicElements.li: React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>
li>
))}
</JSX.IntrinsicElements.ul: React.DetailedHTMLProps<React.HTMLAttributes<HTMLUListElement>, HTMLUListElement>
ul>
);
}