0
State Management · Lifting State Up
New technique
Two components, one truth
Two smiths, one blade. If each keeps their own notes, they'll forge two different swords.
An accordion with two panels: only one may be open at a time. If each Panel keeps its own isOpen state, they can't know about each other.
The fix is lifting state up: move the state to the closest common parent, and pass it down to both panels as props.
- The parent owns the state: one source of truth.
- Children receive the value as props, and a function to ask for changes.
- Keeping two copies in sync with effects is the classic mistake: there's always a moment where they disagree.