Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Resetting state with keys

Learn how a React key resets component state: why state survives at the same position, how changing key forces a fresh instance, and when to prefer it.

React keeps a component's state attached to its position in the tree, not to the component function. If a component renders at the same place with the same type on the next render, React reuses the existing instance and its state, even when the props describe a completely different thing. That is usually what you want, but it surprises people when a form keeps the previous user's draft after the selection changes.

A key tells React which instance is which. When the key changes, React treats the element as a new one: it unmounts the old instance, throws its state away, and mounts a fresh instance that starts from the initial state. Keys are not only for lists. You can put one on any element to say "this is a different thing now".

JSX
// A new Draft instance for every person
<Draft key={personId} name={name} />

Try it

Same position versus a new key

Type something into both fields, then press the other button. The first field keeps your text under the new name. The second one starts empty because its key changed.

Clearing a form after sending

How React decides to keep or reset

React compares the tree from the previous render with the new one, node by node. For each position it asks two questions:

  1. Is the element type the same (Draft and Draft, or div and div)?
  2. Is the key the same (or are both missing)?

If both answers are yes, the existing instance receives the new props and keeps its state. If either answer is no, the old subtree is unmounted (effects cleaned up, state gone) and the new one is mounted from scratch.

This also explains two other situations:

  • Different types at the same spot reset state. Switching {isEditing ? <Form /> : <Preview />} unmounts the one that disappears.
  • Moving a component to a different position resets it. Wrapping an element in an extra <div> changes its place in the tree, so it counts as new.

Choosing a reset strategy

Changing the key is the simplest way to reset everything inside a component. The alternatives are usually worse:

  • Reset in an effect (useEffect(() => setText(''), [id])) renders once with stale state, then again with the reset state, and you must remember every piece of state to clear.
  • Reset during render by comparing the previous prop works, but it is easy to get wrong and only resets what you list.
  • Lift the state into the parent and key its entries by id, when the draft should survive switching back and forth.

Use a key when "different id means a different thing" and nothing inside should survive the switch.

Usage notes

  • Keys must be unique among siblings, not globally. For resetting a single component you can use any string or number, such as a record id or a counter.
  • Do not use a random value (like Math.random()) as a key during render. The key would change on every render and the component would be recreated every time, losing focus and state continuously.
  • Index keys in lists cause the opposite problem: state sticks to the position instead of the item when items are reordered or removed. Use stable ids.
  • A key is not a prop. The component never receives it. Pass the id as a separate prop if the component needs it (see key).
  • Resetting is not free. The whole subtree is recreated: DOM nodes are removed and added, effects run again, and any data fetched inside is requested again.
  • Fragments can have keys too, which is handy when one list item expands to several siblings (see <Fragment>).
  • Scroll position and focus inside the reset subtree are lost with the DOM nodes.