Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint says an element in a list is missing its key prop

ErrorMissing "key" prop for element in iterator react/jsx-key

Short answer

react/jsx-key fires when an element returned from map, Array.from or an array literal has no key. Put a stable key on the element directly inside the loop, use Fragment with a key for multi-node items, and write key before any prop spread.

  • Applies to React 19, eslint-plugin-react 7, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the file. The reported line is the element inside the loop that lacks the key.

    npx eslint src/
  2. 2Match what you see, then jump to its fix

    1. 1A .map callback returns JSX with no key attribute.The mapped element has no keyFix
    2. 2The loop returns <>...</>. Shorthand fragments cannot take a key; the rule flags them when checkFragmentShorthand is enabled.Each item renders several nodes, or a shorthand fragmentFix
    3. 3A list item is a custom component, and the key sits on its root div; or key arrives through {...props} or comes after the spread.The key is on the wrong element, or only inside a spreadFix

What does the error look like?

  • Missing "key" prop for element in iterator react/jsx-key
  • react/jsx-key

Cause 1 of 3

The mapped element has no key

1Check

JSX elements directly inside a map call always need keys. The rule also checks Array.from callbacks and array literals.

grep -rn "\.map(" --include="*.tsx" src | head

2Fix

Add a stable, unique key from your data, such as a database id. Avoid index keys for lists that can reorder, and never use Math.random().

{users.map((user) => (
  <li key={user.id}>{user.name}</li>
))}

3Verify

Run ESLint again on the file.

npx eslint src/components/UserList.tsx

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

Each item renders several nodes, or a shorthand fragment

1Check

Find loops that return a shorthand fragment or sibling elements without a wrapper.

grep -rn "=> *<>\|return *<>" --include="*.tsx" src

2Fix

Import Fragment and put the key on it. Fragments leave no extra element in the DOM.

import { Fragment } from 'react';

{people.map((p) => (
  <Fragment key={p.id}>
    <h2>{p.name}</h2>
    <p>{p.bio}</p>
  </Fragment>
))}

3Verify

Run ESLint; the fragment line is clean.

npx eslint src/

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The key is on the wrong element, or only inside a spread

1Check

The key belongs on the element in the map call, not on the root inside the component. The rule also treats a key that exists only in a spread object as missing, and with checkKeyMustBeforeSpread it flags a key placed after a spread.

grep -rn "{\.\.\." --include="*.tsx" src | head

2Fix

Move the key to the component call and write it before the spread. Pass the id again as a normal prop if the component needs it.

{recipes.map((r) => (
  <Recipe key={r.id} {...r} />
))}

3Verify

Run ESLint again.

npx eslint src/

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Give every record a stable id when you fetch or create it, and use it as the key.
  • Keep react/jsx-key on in your ESLint config so the warning shows in the editor.
  • Use a keyed Fragment, not an extra wrapper div, when a list item has several nodes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.