Missing Key Prop error
The Missing Key Prop error is a very common error that happens when we try to render a list without providing the key
prop to the repeating children.
const Posts = () => {
return (
<ul>
{posts.map((post) => (
<Post post={post} />
// ^ will throw the "Missing Key Prop"
))}
</ul>
);
};
In order to fix it, we need to provide the key
prop that would be unique for every repeating child:
const Posts = () => {
return (
<ul>
{posts.map((post) => (
<Post key={post.id} post={post} />
// ^ the key prop is unique for all posts in this list
))}
</ul>
);
};
Reproduction
The example above will reproduce this error. Open your console to see the output.
- 1
- 2
- 3
- 4
- 5