Skip to content

How to Fix React Missing Key Prop Warning

DodaTech Updated 2026-06-24 3 min read

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 key prop 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-key to warn about index usage.
  • Ensure keys are strings (React converts them anyway, but explicit is better).

Common Mistakes with key prop

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### What happens if I ignore the key prop warning?

React falls back to using the array index as the key, which works but causes performance degradation and potential state bugs. Components may lose internal state (like input values or scroll position) when the list order changes. The warning is there for a good reason.

Can I use Math.random() as a key?

Do not use Math.random() or Date.now() as keys. Keys must be stable across renders. Random keys cause React to destroy and recreate every component on each render, destroying any local state and hurting performance. Keys must be deterministic from the data.

What is the best key for a list of items fetched from an API?

Use the API-provided unique identifier (usually id). If the API does not provide one, compose a key from a combination of fields that together are unique: item.type + '-' + item.slug. For cache-purged lists where data is replaced entirely, use a versioned key like ${dataVersion}-${index}.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro