Skip to content
Back to Pathway
patternsintermediate

State Lifting

Lifting state up involves moving shared state to the closest common ancestor of the components that need it.

Mental Model

"The Common Bulletin Board: Instead of two roommates keeping private notes, they pin messages on the shared refrigerator door."

Interactive Engine Walkthrough

Walkthrough Idle
Click "Animate Update" to play the state reconciliation walkthrough.

Reference Code Snippet

Component.jsx
JSX / React 19
1function Parent() {
2  const [val, setVal] = useState(0);
3  return (
4    <>
5      <SiblingA value={val} onSet={setVal} />
6      <SiblingB value={val} />
7    </>
8  );
9}
Technical Takeaways & Best Practices
  • 01.When two sibling components need to synchronize, lift the state up to their common parent.
  • 02.Parent passes down the state value to one child and the update callback to the other child.
  • 03.Maintains a single source of truth in the component hierarchy.

01. Sharing Memory

Sometimes, two components need to reflect the same changing data. In React, the recommended solution is to "lift" the state up to the **closest common ancestor**. This parent then passes the state back down to both children as props.

Elevation Strategy

Common Parent (State)
Child A (View)
Child B (View)

02. Synchronization by Design

By lifting state, you ensure that the two components are always in sync. There is no possibility of "stale" data because they are both looking at the exact same variable in the parent's memory. This is the "Single Source of Truth" principle in action.

Maintainability Note

"If you lift state too high (e.g. to the root of the app), you'll end up with Prop Drilling. Find the *lowest* possible ancestor that covers all components needing the data. This keeps re-renders local."