How to Fix React Missing Key Prop Warning
In this tutorial, you'll learn about How to Fix React Missing Key Prop Warning. We cover key concepts, practical examples, and best practices.
The Problem
React warns Warning: Each child in a list should have a unique "key" prop when rendering arrays without keys, which hurts performance and can cause incorrect component state preservation during re-renders.
Quick Fix
Step 1: Add a key prop to each iterated element
Rendering a list without keys triggers the warning:
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li>{item.name}</li>
))}
</ul>
);
}
Add a key using the unique id:
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
Step 2: Use a unique identifier, not array index
Using the array index as key can cause issues:
function ItemList({ items }) {
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item.name}</li>
))}
</ul>
);
}
This works for static lists but causes bugs when items are added, removed, or reordered. Use a unique id from the data:
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
Step 3: Use a unique string as a fallback
If the data has no id, combine fields to create a unique key:
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li key={`${item.type}-${item.name}`}>{item.name}</li>
))}
</ul>
);
}
Or generate a unique key:
function ItemList({ items }) {
return (
<ul>
{items.map((item, index) => (
<li key={item.id || `item-${index}`}>{item.name}</li>
))}
</ul>
);
}
Step 4: Apply keys to fragments
When using fragments in a list, apply the key to the fragment:
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<React.Fragment key={item.id}>
<li>{item.name}</li>
<li>{item.description}</li>
</React.Fragment>
))}
</ul>
);
}
Short syntax: <> cannot accept keys, use <React.Fragment> or a <div>.
Prevention
- Always add a
keyprop when using.map()or similar iterations. - Use stable, unique identifiers from your data as keys.
- Avoid using array index as key unless the list is static and never reordered.
- Use ESLint
react/no-array-index-keyto warn about index usage. - Ensure keys are strings (React converts them anyway, but explicit is better).
Common Mistakes with key prop
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro