What if your app's parts could always agree on the latest info without extra work?
Why lifting state is needed in React - The Real Reasons
Imagine you have two separate parts of a webpage that both need to know the same information, like a user's name. You try to keep that information inside each part separately.
Keeping the same information in two places means if you change it in one part, the other part doesn't update automatically. This causes confusion and bugs because the page shows different answers in different spots.
Lifting state means moving the shared information up to a common parent part. This way, both child parts get the same data from one place, so they always stay in sync.
const [name, setName] = useState(''); // in ComponentA and again in ComponentB
const [name, setName] = useState(''); // in ParentComponent, passed down to ComponentA and ComponentB
This lets multiple parts of your app share and update data smoothly, making your app reliable and easier to manage.
Think of a shopping cart where the item count shows in the header and the cart page. Lifting state lets both places show the same number instantly when you add or remove items.
Keeping shared data in one place avoids mismatches.
Lifting state up helps components stay synchronized.
It makes your app easier to understand and fix.
Practice
lifting state means moving state to a common parent component. What is the main reason for this?Solution
Step 1: Understand what lifting state means
Lifting state means moving state from child components to their common parent.Step 2: Identify the purpose of lifting state
This is done so multiple children can share and update the same state, keeping data consistent.Final Answer:
To share the same state between multiple child components -> Option BQuick Check:
Sharing state = lifting state [OK]
- Thinking lifting state is for styling
- Believing lifting state means separate states
- Confusing lifting state with removing props
Solution
Step 1: Recall the pattern of lifting state
State should be moved to the nearest common parent component to share it.Step 2: Check the options for correct syntax
Passing state down as props from the parent is the correct React pattern.Final Answer:
Move the state to the nearest common parent and pass it down as props -> Option CQuick Check:
Lift state = parent state + props down [OK]
- Keeping state only in children
- Using global variables instead of React state
- Removing state completely
function Parent() {
const [count, setCount] = React.useState(0);
return (
<>
</>
);
}
function ChildA({ count }) {
return <div>Count: {count}</div>;
}
function ChildB({ setCount }) {
return <button onClick={() => setCount(c => c + 1)}>Increment</button>;
}What will happen when the button in
ChildB is clicked?Solution
Step 1: Understand state lifting in the code
The statecountis in Parent and passed down to ChildA and ChildB.Step 2: Analyze what happens on button click
Clicking button callssetCountwhich updatescountin Parent, causing ChildA to re-render with new count.Final Answer:
The count displayed in ChildA will increase by 1 -> Option DQuick Check:
Shared state update re-renders children [OK]
- Thinking state is local to ChildB only
- Assuming no re-render happens on state change
- Confusing props with state setters
Solution
Step 1: Understand React state sharing rules
State lives inside a component and cannot be directly shared between sibling components.Step 2: Identify correct sharing method
To share state, it must be lifted to the nearest common parent, which passes it down as props.Final Answer:
Because siblings cannot directly share state; state must be lifted to their parent -> Option AQuick Check:
Siblings share state via parent [OK]
- Thinking siblings can share state directly
- Believing React forbids state in functional components
- Confusing props passing between siblings
Grandparent, Parent, and two siblings Child1 and Child2. Both children need to update and display the same piece of state. Where should you lift the state to and why?Solution
Step 1: Identify the nearest common ancestor of the siblings
The siblingsChild1andChild2share theParentcomponent as their closest common parent.Step 2: Decide where to lift state for sharing
Lifting state toParentallows both children to access and update the same state easily without unnecessary complexity.Final Answer:
Lift the state toParentbecause it is the nearest common ancestor of the children -> Option AQuick Check:
Lift state to nearest common parent [OK]
- Lifting state too high unnecessarily
- Trying to share state directly between siblings
- Duplicating state in multiple components
Ready to prove it?
Pass the test and this pattern moves unknown
