Each child in a list should have a unique "key" prop
The Warning: Each child in a list should have a unique ”key" prop happens in react when we create a list of elements without a key for each element. React use this unique key mainly for performance improvements. Keys must be unique across all siblings. We can use either an id property on the passed object or use the auto-assigning unique keys.
The id property of the object as a unique key
We can set the id property of the object as a unique key.
export default function App() {
const posts = [
{ id: 1, title: "First Post"},
{ id: 2, title: "Second Post" },
{ id: 3, title: "Third Post" }
];
return (
<div>
<ul>
{posts.map(post =>
<li key={post.id}>{post.title}</li>
)}
</ul>
</div>
);
}
In the above example, we use the id property of the post object as a key for <li>
.
{posts.map(post =>
<li key={post.id}>{post.title}</li>
)}
Auto-assigning unique keys
import React from 'react';
export default function App() {
const posts = [
{ id: 1, title: "First Post"},
{ id: 2, title: "Second Post" },
{ id: 3, title: "Third Post" }
];
return (
<div>
<ul>
{React.Children.toArray(
posts.map(( post ) => <li>{post.title}</li>)
)}
</ul>
</div>
);
}
React.Children.toArray method is a better solution to fix this issue.
React.Children.toArray() returns the children opaque data structure as a flat array with keys assigned to each child. The function toArray
prefixes each key in the returned array so that each element’s key is scoped to the input array containing it.
Why each element in a list should have a unique key?
React use this unique key to identify each element in a list uniquely. Unique keys are important for performance improvements. When we insert, delete, or update the list, React re-renders the array elements. In this case, React uses the key to identify each item. Therefore, React only re-renders where modification is needed and, no re-rendering happens to existing items. The key should be unique and remain the same during each re-render. Random numbers should never be assigned as keys.
Read how React reconciliation works.
Why an index of the array as the key prop is not recommended?
In the below example, we use the index of the array as a key for child elements.
{posts.map((post, index) =>
<li key={index}>{post.title}</li>
)}
This solution is not recommended for arrays that perform inserting, deleting, or sorting the items. Because the key should remain the same during each re-render. When we reorder items in an array, the index of items may get changed. Then React will get confused and re-render the incorrect element.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
Build password strength checker in React
Can't Perform a React State Update on an Unmounted Component
Create a Back button with React Router
Programmatically update query params in React Router
The style prop expects a mapping from style properties to values, not a string
Adjacent JSX elements must be wrapped in an enclosing tag
How to create a scroll to top button in React
Too many re-renders. React limits the number of renders to prevent an infinite loop
Explore All Blogs