Skip to article content
FrontendXLesson 11 / 40

Keys, Identity and Reconciliation in Lists

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

intermediate · about 50 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Explain how keys drive reconciliation of list children.
  • Choose a stable key and recognize when index keys break.
  • Use a key change to reset a component's state deliberately.

Overview

#

Identity is the reconciliation question

When React compares the previous list with the next one, it must decide which old element corresponds to each new element. Keys are how you answer that question. A key is a stable string that identifies an item across renders, so React can move the existing DOM node and keep its state instead of guessing by position.

snippet.tsx
{items.map((item) => (
  <Row key={item.id} item={item} />
))}

React matches children of the same parent by key first, then by position. Keys must be unique among siblings, not globally.

Why index keys break

An index key ties identity to position. After a sort, insert or delete, position no longer matches the item, so React reuses the wrong DOM node and the wrong component state. A controlled input can show another row's text, and focus can jump to the wrong field.

snippet.tsx
// fragile: identity follows position
{items.map((item, index) => <Row key={index} item={item} />)}

Keys reset state on purpose

Changing a key unmounts the old element and mounts a new one. That is a feature when you want a clean slate, for example to reset a form when the selected record changes. Pass a key that changes with the data instead of writing an effect that copies props into state.

snippet.tsx
<ProfileForm key={user.id} user={user} />

Pitfall: keys on fragments

A fragment used as a list item needs its own key, and the shorthand syntax cannot take one. Use Fragment with an explicit key when mapping a list of fragments.

snippet.tsx
{rows.map((row) => (
  <Fragment key={row.id}>
    <dt>{row.term}</dt>
    <dd>{row.definition}</dd>
  </Fragment>
))}

Choosing a key

Prefer a stable id from the data. If none exists, create one when the item is created and keep it with the item. Only fall back to the index when the list is static, never reorders and never changes length.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    The list is reversed. What happens to the text typed into the first input when index keys are used?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    function List({ items }: { items: string[] }) {
      return (
        <ul>
          {items.map((item, index) => (
            <li key={index}>
              <input defaultValue={item} />
            </li>
          ))}
        </ul>
      );
    }
    Choose the best answer
  2. Exercise 2Implement

    Render a list of todos with stable keys so that toggling one todo does not disturb the others.

    starter.tsx
    type Todo = { id: string; text: string; done: boolean };
    
    export function TodoList({ todos }: { todos: Todo[] }) {
      return null;
    }

    Reveal the reference answer
    reference.tsx
    type Todo = { id: string; text: string; done: boolean };
    
    export function TodoList({ todos }: { todos: Todo[] }) {
      return (
        <ul>
          {todos.map((todo) => (
            <li key={todo.id}>{todo.text}</li>
          ))}
        </ul>
      );
    }

    Authored expected output: Each todo renders once and keeps its identity when the list changes.

    Using todo.id as the key ties identity to the item, so React moves the existing node when the order changes instead of reusing a neighbour's node.

  3. Exercise 3Debug

    This list highlights the wrong row after a delete. Identify the cause and fix it.

    starter.tsx
    {items.map((item, index) => (
      <Row key={index} selected={item.id === selectedId} item={item} />
    ))}

    Reveal the reference answer
    reference.tsx
    {items.map((item) => (
      <Row key={item.id} selected={item.id === selectedId} item={item} />
    ))}

    Authored expected output: After deleting a row, the correct row stays highlighted.

    Index keys bind identity to position. After a delete, React reuses the node at each index for a different item, so local state such as highlight follows the position instead of the item. A stable item.id key fixes the match.

  4. Exercise 4Explain

    Explain why changing a component's key resets its state, and give one case where that is desirable.

    starter.tsx
    <ProfileForm key={user.id} user={user} />

    Reveal the reference answer
    reference.tsx
    A key is part of a component's identity during reconciliation. When the key changes, React sees a different element at that position, unmounts the old component and mounts a fresh one, so all local state is recreated from initial values. This is desirable when you want a form to reset for a new record without an effect that copies props into state.

    Authored expected output: A written explanation of key-as-identity and the unmount/mount consequence, plus a concrete use case.

    Keys drive whether React updates or replaces an element. Replacing discards state, which is exactly what a reset needs. The alternative, an effect that sets state from props, shows a stale frame and is more code.

Common misconceptions

#
  • The array index is a safe key as long as the list is short. Length is irrelevant; any reorder or deletion makes index keys wrong.
  • Keys must be globally unique. Keys only need to be unique among siblings in the same parent.
  • Changing a key is a bug because it remounts. A key change is the intended way to reset a component's state.
  • React writes the key to the DOM id attribute. Keys are internal to reconciliation and never reach the DOM.

Follow-up practice

#
  • react-state-colocation
  • react-list-virtualization

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.