To reset a component's state in React, give it a different key. When the key changes, React unmounts the old instance and mounts a fresh one, so all of its state, including state of its children, starts from the initial values. It is the cleanest way to reset state without writing reset code.
This guide explains why key has this effect, shows common uses such as switching between profiles, and compares the alternatives. Examples use React 19.
React ties state to a component's position in the tree and its key. If the same component type appears at the same position with the same key, React keeps its state. If the key changes, React treats it as a different component and discards the old one.
Rendering <Profile userId={1} /> and then <Profile userId={2} /> in the same place keeps the draft text typed in a form inside it. Often that is a bug.
Pass the identity of the data as the key.
Without key, switching users would leave the old name and comment in the form, because useState(user.name) only reads its argument on the first render. With key={user.id}, each switch creates a new EditProfile with its state initialised from the new user.
Most guides show key on list items, but it is a general identity hint. You can use it on any element or component, and it is also the standard fix for stale form data.
Incrementing a counter used as a key is a quick way to clear a form after a successful submit. Choose a meaningful key, such as an id, so the reset happens exactly when the data identity changes.
Changing a key remounts the whole subtree. That has consequences:
For a form with a few fields this is cheap and correct. For a large subtree, consider the alternatives below.
| Approach | Use when | Drawback |
|---|---|---|
key on the component | State should fully reset when identity changes | Remounts the subtree |
| Reset state in an effect on prop change | Almost never | Extra render with stale UI, easy to get wrong |
| Store only an id and derive the rest | One field depends on a prop | Needs restructuring |
| Adjust state during render | A single value must follow a prop | Easy to misuse, must be guarded |
Resetting state in useEffect shows one render with the old state, then another with the reset one. The key approach avoids that flash because React throws the old state away before rendering.
You do not need to key the whole page. Put the key on the smallest component that owns the state to reset, and keep other state, such as a sidebar scroll position, outside it.
Here only the Editor resets when documentId changes.
Math.random() or a fresh crypto.randomUUID() on every render as a key. The component remounts on every render and loses state constantly.Yes. React runs cleanup for effects, removes the old instance and mounts a new one with fresh state.
You can write a reset function that sets each state variable to its initial value. This keeps DOM nodes but is more code and easy to leave incomplete.
No. key is reserved. Read it from your own props if the child needs the value.
Yes. Stable keys let React match each item to its state when the list reorders.
Changing the key of a component resets its whole subtree state, because React sees a new component. Key by the identity of the data, put it on the smallest component that needs a reset, and prefer it to effects that copy props into state.