Reset state using identity
State follows a component identity
React associates local state with a component in the rendered tree. A new prop does not by itself mean a new component instance. At a matching position, a compatible component type and key can retain state. Removing a component, changing its type, or changing its key can discard that state. Consider a note editor whose note should reset when the selected workshop changes.
import { useState } from 'react';
function NoteEditor({ workshop }) { const [note, setNote] = useState(''); return <label>Note for {workshop}<textarea value={note} onChange={e => setNote(e.target.value)} /></label>;}export default function App() { const [workshop, setWorkshop] = useState('Drawing'); return <main><h1>Shared scratchpad</h1> <button type="button" onClick={() => setWorkshop(w => w === 'Drawing' ? 'Pottery' : 'Drawing')}> Switch workshop</button><NoteEditor workshop={workshop} /> </main>;}Type a note and switch workshop. The label changes but the text remains because NoteEditor is still the same type at the same place. This is useful for a shared scratchpad but wrong if each selection requires a fresh form. It is not evidence that separate component instances share state.
Use a stable identity to request a reset
import { useState } from 'react';
const workshops = [{ id: 'drawing', title: 'Drawing' }, { id: 'pottery', title: 'Pottery' }];function NoteEditor({ workshop }) { const [note, setNote] = useState(''); return <label>Note for {workshop}<textarea value={note} onChange={e => setNote(e.target.value)} /></label>;}export default function App() { const [selectedId, setSelectedId] = useState('drawing'); const workshop = workshops.find(w => w.id === selectedId); return <main><h1>New workshop note</h1><p>Switching discards this unsaved note.</p> <label>Workshop <select value={selectedId} onChange={e => setSelectedId(e.target.value)}> {workshops.map(w => <option key={w.id} value={w.id}>{w.title}</option>)} </select></label><NoteEditor key={workshop.id} workshop={workshop.title} /> </main>;}Write a note for Drawing and select Pottery: the old editor is replaced, and its new text starts empty. Selecting Drawing again also starts empty, not the previously entered note. Keys identify siblings within a parent; they are not globally unique instance IDs. Key changes can reset the whole subtree, including DOM state and focus. If users expect drafts to survive switching, store drafts by workshop ID in the parent instead. Do not generate a random key each render.
Removing or replacing a subtree also resets it
import { useState } from 'react';
function Note() { const [text, setText] = useState(''); return <label>Draft <input value={text} onChange={e => setText(e.target.value)} /></label>;}function Preview() { return <p>Read-only preview</p>;}export default function App() { const [hidden, setHidden] = useState(false); const [mounted, setMounted] = useState(true); const [preview, setPreview] = useState(false); return <main><h1>Draft lifecycle</h1> <button type="button" onClick={() => setHidden(v => !v)}>Toggle visibility</button> <button type="button" onClick={() => setMounted(v => !v)}>Toggle presence</button> <button type="button" onClick={() => setPreview(v => !v)}>Toggle preview</button> <div hidden={hidden}>{mounted && (preview ? <Preview /> : <Note />)}</div> </main>;}With the initial settings, type a draft. Toggle visibility twice: the input remains mounted and retains its value. Toggle presence off and on: Note is removed and recreated, so its value is empty. Type again, switch to Preview, then back: the component type changes at that position, and Note starts fresh. Hiding keeps the subtree alive; it is not a cleanup mechanism. Define component types outside their parent render to avoid accidental identity changes.
Practice
Design a two-workshop editor that preserves each draft when switching. Sketch where the draft map belongs and how the currently selected note gets its value and change callback. Contrast this with a keyed reset.