React Component Identity: Preserving State and Resetting It with Keys
Understand how tree position, element type, and keys determine whether React keeps or recreates component state.
React associates component state with a position in the rendered tree. Component type and key help define that identity, so changing a key can intentionally reset a subtree while stable keys preserve state as list items move.
THE PRODUCTION PROBLEM STATEMENT
A form can retain one user’s values when the screen switches to another record, or lose edits when list items reorder. These bugs come from confusing a component’s props with the identity React uses to preserve state.
State follows a tree position and component identity
React keeps state when a component continues to occupy the same position with the same type and key. Rendering different props does not automatically reset its state, because React interprets that as the same component receiving new inputs.
This is useful when a component represents one continuing UI element. When the underlying entity changes and its local state should start fresh, make the identity change explicit.
function ProfileRoute({ userId }: { userId: string }) {
return <ProfileEditor key={userId} userId={userId} />;
}
function ProfileEditor({ userId }: { userId: string }) {
const [draft, setDraft] = useState('');
return <textarea value={draft}
onChange={(event) => setDraft(event.target.value)} />;
}Use stable data keys when rendering collections
A key lets React match an item with its previous instance when items are inserted, removed, or reordered. Use a stable identifier from the data. Array indexes can cause local state and focus to follow the position instead of the item.
Keys only need to be unique among siblings. They are reconciliation metadata and are not passed to the child as a regular prop; pass an identifier separately if the child needs it.
function TaskList({ tasks }: { tasks: Task[] }) {
return tasks.map((task) => (
<TaskRow key={task.id} taskId={task.id} task={task} />
));
}Prefer identity changes over effect-based reset code
An effect that watches a new record ID and clears several state variables renders once with old state before the effect runs. A changed key lets React discard the old subtree state as part of the identity change.
Do not key a component with a value that changes on every render. Remounting discards focus, DOM state, and descendant state, so reserve it for a meaningful boundary such as switching records or restarting a wizard.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. It remounts that component identity and resets state throughout the keyed subtree.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Under the Hood of React Fiber: Work Loops, Reconciliation Phases, and Lane-Based Priority Scheduling
A mechanical examination of the Fiber node tree, the double-buffering work loop, and how React yields to browser frames using 31-bit integer lanes.
React Memoization Pitfalls: When useMemo and useCallback Actually Hurt Performance
A pragmatic examination of shallow comparison overhead, closure retention, cache churn, and why premature memoization degrades React application speed.