Predict row state after reordering
Predict what happens to a row when it moves
State is information remembered between renders, such as a draft note or a counter. React associates component state with its place in the component tree, considering the parent, component type and key. For siblings of the same type under the same parent, a stable record key lets React keep the corresponding row when its position changes. A changed key can reset that row. Here we preview state and event handlers only to demonstrate identity: useState returns the current value and an update function, and onClick receives a function to run when the button is activated. Full lessons on events, state, and forms follow.
import { useState } from 'react';
const exhibits = [{ id: 'a', name: 'Moon map' }, { id: 'b', name: 'River model' }];
function Row({ exhibit }) { const [note, setNote] = useState(''); return <li><label>{exhibit.name} note <input value={note} onChange={event => setNote(event.target.value)} /></label></li>;}
export default function App() { const [reversed, setReversed] = useState(false); const rows = reversed ? [...exhibits].reverse() : exhibits; return <main><h1>Exhibit notes</h1> <button type="button" onClick={() => setReversed(!reversed)}>Reverse order</button> <ul>{rows.map((exhibit, index) => <Row key={index} exhibit={exhibit} />)}</ul> </main>;}Try this in an approved browser project: type Check frame beside Moon map and activate Reverse order. Predicted: River model is now first, but the draft is still associated with the row keyed 0, so the text is displayed beside the wrong exhibit. The input is controlled: its displayed value comes from note, and its change handler updates note. The spread expression copies the array before reverse changes the order; the original fixture is not mutated.
Keep each draft with its exhibit
import { useState } from 'react';
const exhibits = [{ id: 'a', name: 'Moon map' }, { id: 'b', name: 'River model' }];
function Row({ exhibit }) { const [note, setNote] = useState(''); return <li><label>{exhibit.name} note <input value={note} onChange={event => setNote(event.target.value)} /></label></li>;}
export default function App() { const [reversed, setReversed] = useState(false); const rows = reversed ? [...exhibits].reverse() : exhibits; return <main><h1>Exhibit notes</h1> <button type="button" onClick={() => setReversed(!reversed)}>Reverse order</button> <ul>{rows.map(exhibit => <Row key={exhibit.id} exhibit={exhibit} />)}</ul> </main>;}Predicted: the typed draft moves with Moon map to the second row, and River model retains its own draft. Only the key expression changed. The row type and parent remain the same. If a row is removed entirely, its local state is not a durable store; reintroducing that ID later generally creates a new row. To retain drafts across removal, store them in an owner that stays mounted, a topic covered later.
Use a changed key for an intentional reset
import { useState } from 'react';
function Note({ name }) { const [draft, setDraft] = useState(''); return <label>{name} note <input value={draft} onChange={event => setDraft(event.target.value)} /></label>;}
export default function App() { const [second, setSecond] = useState(false); const exhibit = second ? { id: 'b', name: 'River model' } : { id: 'a', name: 'Moon map' }; return <main><h1>One exhibit note</h1> <button type="button" onClick={() => setSecond(!second)}>Switch exhibit</button> <Note key={exhibit.id} name={exhibit.name} /> </main>;}Predicted: switching the selected exhibit clears the draft; switching back also starts a fresh draft. This is intentional replacement, not an update to the original Note state. A key is not a mechanism to save data or guarantee DOM focus. Use an intentional reset only when discarding the old draft is acceptable. For real unsaved work, consider warning or preserving drafts. These fixtures have not been rendered here; the browser checklist specifies the expected observations.
Practice
Make a two-row equipment checklist with local note text. Predict the outcome after reversing the order using index keys, then stable IDs. Explain the difference between reordering a retained row and removing it from the tree. Perform the comparison in an approved browser environment.