How do you reset a component state in React?
Start with component identity
State survives rerenders because React associates it with a component's position in the render tree, its type, and its key within the parent. Calling the same component function again does not create fresh state. Its useState initial value initializes a mount; changing that argument later does not reinitialize existing state. Removing a component destroys its local state. Changing its type or key replaces the instance.
| Approach | Useful when | Consequence |
|---|---|---|
| Reset selected state with setters or a reducer action | Only selected fields should change | The instance and unrelated state survive |
| Change the component key | The whole instance should start fresh | Descendant state, effects, and DOM state reset too |
| Lift state to a parent | Drafts must survive child removal or switching | The parent owns persistence and cleanup |
Reset a selected field
import { useState } from 'react';function MessageDraft() {const [text, setText] = useState('');const [showHelp, setShowHelp] = useState(false);return <><textarea aria-label="Message" value={text}onChange={e => setText(e.target.value)} /><button onClick={() => setText('')}>Clear draft</button><button onClick={() => setShowHelp(value => !value)}>Toggle help</button>{showHelp && <p>Keep your message concise.</p>}</>;}
Clearing the draft preserves the help preference. A reducer's reset action offers the same control for many related fields. Avoid mutating a shared initial object; create new state when editing it.
Chat recipients: reset or preserve deliberately
import { useState } from 'react';function Chat({ recipient }) {const [draft, setDraft] = useState('');return <label>Message to {recipient.name}<textarea value={draft} onChange={e => setDraft(e.target.value)} /></label>;}export function ChatPanel({ recipient }) {return <Chat key={recipient.id} recipient={recipient} />;}
Switching from Alice to Bob replaces Chat, so Alice's unsent message cannot silently become Bob's draft. Returning to Alice also creates an empty draft. If retaining a draft per recipient is the product requirement, lift drafts into the parent as a map indexed by recipient ID and pass the selected draft and setter down. A key by itself does not archive old instances.
Reopening a form for another record
import { useState } from 'react';function RecordForm({ record }) {const [name, setName] = useState(record.name);return <label>Name<input value={name} onChange={e => setName(e.target.value)} /></label>;}export function Editor({ record, open }) {if (!open) return null;return <RecordForm key={record.id} record={record} />;}
Changing record ID while the editor is open resets the form to the new record's initial name. Closing this conditional editor removes RecordForm, so reopening even the same record creates fresh local state. Merely hiding the form with CSS keeps it mounted and preserves its draft. If reopening must preserve edits, lift them; if a mounted form needs an explicit fresh session for the same record, use a session counter as part of its key. A server refresh with the same record ID will not overwrite an in-progress draft automatically. Decide whether to preserve edits, merge updates, or offer an explicit reload.
Keys have a wider cost
Replacing an instance resets descendant local state and creates new DOM elements, which can lose focus, selection, scroll position, and uncontrolled input values. Effect cleanup tears down old resources and setup establishes new ones. Use a stable semantic ID, not Math.random() or the current timestamp on every render. Confirm or save unsaved edits before intentionally switching records.
Nested definitions cause accidental resets
// Problem: each parent render creates a different component type.function Editor() {function Field() { return <input defaultValue="Draft" />; }return <Field />;}// Correction: define Field at module scope and pass data as props.function Field({ initialValue }) { return <input defaultValue={initialValue} />; }function StableEditor() { return <Field initialValue="Draft" />; }
Typing can appear to reset when a parent update causes the nested component to be replaced. Ordinary helper functions returning elements do not have the same component-type identity issue; the problem here is rendering a newly defined function as a component.
Avoid a delayed whole-form reset
An effect that clears fields after a record prop changes first commits the new record with old local values, then requests another render. A key expresses a full identity change directly. For a selective update, keep state ownership explicit and use an event or a reducer transition where appropriate. Do not reset every form automatically: preserving user input is often the desired behavior.
Interview follow-ups
- Why does rerendering preserve state but changing a key reset it?
- Should Bob get a blank chat or a previously saved draft?
- Does hiding a modal preserve state? What changes if it unmounts?
- What else besides
useStatedisappears when this subtree is replaced?
Reviewed 1 October 2026 against React: preserving and resetting state.
Choose the intended state lifetime
Write the desired behavior before choosing a reset mechanism.
- Clear selected fields with setters or a reducer reset action.
- Use a semantic key when a different entity needs a completely fresh instance.
- Lift drafts when they must survive switching or child removal.
- Account for lost focus, DOM state, and descendant cleanup when remounting.
Related interview practice
Related React Ecosystem interview topics
Continue with nearby questions from the same topic cluster.