Identity: position, type, key

React chapter 140 · Reconciliation · lists · where state lives · source: react.dev/learn/preserving-and-resetting-state

The problem

A list of people, each row with its own note. The rows use key={index}. Remove the first person and every note slides onto the wrong person, because React matched old and new rows by position.

The pattern that fixes it

State belongs to a component’s place in the tree, and key is how you name that place. With key={person.id} React matches rows by identity: the removed row’s state is destroyed and every other note stays with its owner.

In plain words

React does not know which row is “Ada”. It only sees “first row, second row…”. A key is a name tag that lets React recognise the same item after the list changes.

Coats on numbered hooks. If the first coat leaves and everyone shifts one hook left without name tags, the cloakroom hands you someone else’s coat.

Terms

reconciliation
React comparing the new tree to the previous one to decide what to keep, update or recreate.
identity
Same component type in the same position (and with the same key) = the same instance, keeping its state.
key
A stable, unique id among siblings that tells React which item is which.

Going deeper

The three rules React uses

1) Different type at a position (div → span, A → B): destroy the subtree and its state, mount fresh. 2) Same type: keep the instance, update props. 3) Among siblings, match by key when present, else by index. That is the whole algorithm, and it is O(n) because React never searches for moved subtrees without keys.

Index keys are fine… sometimes

If the list never reorders, inserts or deletes in the middle, and rows have no state or uncontrolled inputs, index keys work. The moment any of that is false, they silently attach state to the wrong item. Random keys (Math.random()) are worse: every render remounts everything.

Using identity on purpose

Because a new key means a new instance, <Form key={userId} /> resets a form when the user changes (chapter 93). And defining a component inside another component creates a new type each render, which remounts it every time (chapter 92).

Keep learning

Overview

likes tea on holiday owes €5 new joiner Remove first Reverse order People with notes Real life: editable table rows, todo items with checkboxes, chat messages with drafts.

The notes stay put; the people move.

Each note starts with its owner’s name. After a delete or a reverse, red rows show state that now belongs to someone else. Press Remove first. Bea now shows Ada’s note, and Dan’s note is the one that vanished. Matching old rows to new rows by index versus by key Ada: tea Bea: hol remove Ada → new list: [Bea, Cy] ✕ destroyed Ada ✕ destroyed same type + same key/position Same rows. Give each one a name tag React can recognise. How React matches old rows to new rows Before: three <Row> instances. Each holds its own note in state. You remove Ada. React now has two new elements and must decide which old instance each one is. The whole rule: same component type at the same key (or position) is the same instance, with the same state.

Keys from your data, never from the array position.

With person.id, remove and reverse as much as you like: every row stays green.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.