0
0
Reactframework~10 mins

Common lifting state patterns in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common lifting state patterns
Child A needs state
State lifted to Parent
Parent holds state
Parent passes state and setter to Child A
Child A updates state via setter
Parent updates state and re-renders
Updated state passed down to Child A and Child B
Child B uses updated state
State is moved up to the parent so multiple children can share and update it, keeping data in sync.
Execution Sample
React
import React, { useState } from 'react';

function Parent() {
  const [count, setCount] = useState(0);
  return (
    <>
      <ChildA count={count} setCount={setCount} />
      <ChildB count={count} />
    </>
  );
}
Parent component holds count state and passes it with setter to ChildA and count to ChildB.
Execution Table
StepActionState BeforeState AfterComponent RenderedOutput/Behavior
1Parent initializes count=0count=undefinedcount=0ParentRenders ChildA and ChildB with count=0
2ChildA renders with count=0count=0count=0ChildAShows count=0, has button to increment
3ChildB renders with count=0count=0count=0ChildBShows count=0
4User clicks increment in ChildAcount=0count=0ChildACalls setCount(1)
5Parent updates count to 1count=0count=1ParentTriggers re-render of Parent and children
6ChildA re-renders with count=1count=1count=1ChildAShows updated count=1
7ChildB re-renders with count=1count=1count=1ChildBShows updated count=1
8No more actionscount=1count=1NoneState stable, UI updated
💡 User stops interaction; state is stable and UI reflects latest count.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
countundefined0011
Key Moments - 3 Insights
Why does ChildB update even though it doesn't have a setter?
Because Parent holds the state and passes count as a prop to ChildB, when Parent updates count, ChildB re-renders with new value (see steps 5-7).
Why do we lift state to Parent instead of keeping it in ChildA?
Lifting state to Parent allows multiple children (ChildA and ChildB) to share the same state and stay in sync, as shown in the flow from step 1 to 7.
What happens if ChildA calls setCount with the same value as current state?
React will not re-render because state didn't change, so ChildB won't update either (not shown in table but important to know).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 5?
A1
B0
Cundefined
D2
💡 Hint
Check the 'State After' column at step 5 in the execution_table.
At which step does ChildB first render with the updated count?
AStep 6
BStep 7
CStep 3
DStep 4
💡 Hint
Look for ChildB rendering with count=1 in the execution_table rows.
If we remove setCount from ChildA props, what changes in the execution?
AChildB stops rendering
BParent state updates automatically
CChildA cannot update count, so count stays 0
DNo change, everything works the same
💡 Hint
Refer to step 4 where ChildA calls setCount to update state.
Concept Snapshot
Lifting state means moving state up to a common parent.
Parent holds the state and passes it down as props.
Children receive state and setter functions as needed.
Updating state in parent triggers re-render of all children.
This keeps shared data consistent across components.
Full Transcript
In React, lifting state means moving the state from a child component up to their common parent. This allows multiple children to share and update the same state. The parent holds the state and passes it down as props to children. One child can receive the state and a setter function to update it. When the setter is called, the parent updates the state and re-renders. This causes all children receiving that state to update their display. This pattern keeps data consistent and synchronized across components that need to share it.