React keeps a component's state as long as the same component type is rendered at the same position in the tree. To reset it, make React see a different identity: the idiomatic way is changing its key (<Form key={userId} />), which unmounts the old instance and mounts a fresh one. Other options: render it at a different position, explicitly set state back to initial values (often via a reducer reset action), or, for forms, form.reset() / React 19 form Actions, which reset uncontrolled fields automatically.
function Inbox({ contactId }) {
return <ChatDraft key={contactId} contactId={contactId} />;
}
function ChatDraft({ contactId }) {
const [text, setText] = useState('');
// When contactId changes, this is a brand-new instance: text resets to ''
return <textarea value={text} onChange={e => setText(e.target.value)} />;
}
This replaces the anti-pattern of syncing with an effect:
// Avoid: renders once with stale state, then resets, and is easy to get wrong
useEffect(() => { setText(''); }, [contactId]);
Remember that a key change remounts the whole subtree: child state is lost, effects clean up and re-run, and DOM nodes are recreated. That's correct for "this is a different entity" and wasteful for big trees that only need one field reset.
{isPlayerA ? <Counter key="a" /> : <Counter key="b" />}
// or
{isPlayerA && <Counter />}
{!isPlayerA && <Counter />}
Two <Counter />s in the same JSX slot of a ternary without keys share state, because same type at the same position means the same instance. Rendering them in different slots, or giving them keys, separates them.
const initial = { name: '', email: '' };
function reducer(state, action) {
switch (action.type) {
case 'changed': return { ...state, [action.field]: action.value };
case 'reset': return initial;
}
}
Use this when only part of the state should reset, or when you want to keep the DOM (and focus or scroll position).
If you need to reset some state in response to a prop change without remounting, react.dev describes storing the previous prop and updating state during render:
const [prevId, setPrevId] = useState(id);
if (id !== prevId) {
setPrevId(id);
setSelection(null);
}
React re-renders immediately without committing the stale output. It's better than an effect, but a key is usually clearer.
formRef.current.reset() restores defaultValues.<form action={fn}> Action succeeds, React automatically resets uncontrolled fields. requestFormReset(form) from react-dom triggers that manually.key={Math.random()} resets on every render.<div> → <section> wrapping it) also resets everything below.key as the idiomatic reset tool, and why the effect-based reset is an anti-pattern.Spotted a mistake or a rights concern? Report an issue