Most React developers meet key inside a list, where it helps React match elements across renders. But key has a second, far more powerful job that rarely gets the attention it deserves: it is the identity of a component instance. When React sees a different key in the same position of the tree, it tears down the old instance — local state, DOM, effects, the lot — and mounts a brand-new one.
That makes key the cleanest, most explicit way to reset a component without lifting its state upward.

The Problem: State That Refuses to Go Away
Picture a multi-step form. Each step is its own component holding local state — inputs, validation flags, a draft object. The user fills step 1, moves to step 2, then clicks “back” to step 1 and expects a fresh, empty form.
If the same component instance is reused, that old state is still sitting there. The naive fix is to hoist every field into the parent and clear it by hand. It works, but it is tedious and a magnet for bugs. There is a simpler tool already in your toolbox.
The Fix: Change the key
Wrap the step and give it a key that changes exactly when you want a clean slate:
import { useState } from "react';
function App() {
const [step, setStep] = useState(1);
return (
<div>
<Stepper current={step} onNext={() => setStep(s => s + 1)} />
{/* Changing `step` gives FormStep a brand-new key,
so React unmounts the old instance and mounts a fresh one. */}
<FormStep key={step} step={step} />
</div>
);
}
function FormStep({ step }) {
const [draft, setDraft] = useState({ name: '', email: '' });
const [saved, setSaved] = useState(false);
// Every time `step` changes, this component remounts:
// `draft` and `saved` both snap back to their initial values.
return (
<form>
<input
value={draft.name}
onChange={e => setDraft(d => ({ ...d, name: e.target.value }))}
/>
<input
value={draft.email}
onChange={e => setDraft(d => ({ ...d, email: e.target.value }))}
/>
</form>
);
}
Because key={step}, the path 1 → 2 → 1 does not reuse the first instance. React sees key "1", then "2", then "1" again — and the "1" on the way back is a new mount, not a recycled one. Local state resets for free, no parent plumbing required.
A Real Gotcha: Index Keys Hiding State Bugs
The flip side is genuinely dangerous. Using the array index as a key is fine for static, append-only lists, but it backfires the moment the list order changes:
// ❌ Bug: deleting item 0 shifts everything, but React reuses
// the instances — so local state rides to the WRONG row.
{todos.map((todo, i) => (
<TodoItem key={i} todo={todo} />
))}
If todos is ["a", "b", "c"] and the user deletes "a", React shifts index 1 → 0 and 2 → 1. The component that was rendering "b" now renders "c", but it never remounted — so any in-progress local edit silently moves to the wrong item. The fix is stable identity:
// ✅ A stable id survives reordering and deletion
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} />
))}
When Not to Reach for key
- Don’t use a changing
keyto reset state that should actually survive the change. If the user’s in-progress input must persist, you want the same instance — keep a stablekey. - Never generate a fresh
keyon every render (key={Math.random()}). That remounts on every render, torching performance and stealing focus. - If several siblings genuinely need to share state, lift it up instead.
keyremount is a reset, not a share.
Takeaway
key is not just a list optimization. It is React’s identity signal:
- same
key→ same instance → state preserved - different
key→ fresh instance → state wiped
Reach for a changing key whenever you need a clean reset, and protect your lists with stable IDs to dodge silent state-migration bugs.