LLearnCode
Academy/react/Lifecycles and production reasoning

advanced · 23 minute study

Keys tell React which entity keeps its state

Index or random keys can move editable row state to the wrong item or reset it unexpectedly.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

First, build a mental model

React associates state with a component's identity in the render tree. A stable key helps distinguish siblings as their order changes.

  1. Keys must be stable and unique among siblings.
  2. Use entity IDs when available.
  3. An array index describes a position, not necessarily an entity.
  4. Changing a key deliberately resets that component's state; random keys do this accidentally on every render.
Read the codejsx · educational example
function PlayerList({ players }) {
  return players.map(player =>
    <EditablePlayer key={player.id} player={player} />
  );
}
// Risky when items reorder: key={index}
// Risky on every render: key={Math.random()}
  1. Before reorder

    Row state belongs to each keyed player.

  2. After reorder

    Stable keys let that state move with the player.

  3. Changed key

    A different identity causes a fresh component state.

Production thinking

Take this beyond the example.

Test row insertions, deletions, sorting and filtering with partially edited inputs. A static screenshot cannot reveal state-identity bugs.

Sources and further reading

Your evidence

unseen is a local learning signal, not a credential or proof of production competence.

Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.

Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.

Next lesson · Measure work before caching it →