Lifting state helps share information between components in React. It moves state up to a common parent so multiple parts can use it.
Why lifting state is needed in React
import React, { useState } from 'react'; function Parent() { const [sharedState, setSharedState] = useState(initialValue); return ( <> <ChildA state={sharedState} setState={setSharedState} /> <ChildB state={sharedState} /> </> ); }
The state is declared in the parent component.
State and its updater function are passed down as props to children.
import React, { useState } from 'react'; function Parent() { const [count, setCount] = useState(0); return ( <> <Incrementer count={count} setCount={setCount} /> <Display count={count} /> </> ); }
import React, { useState } from 'react'; function Parent() { const [text, setText] = useState(''); return ( <> <InputBox text={text} setText={setText} /> <Preview text={text} /> </> ); }
This example shows lifting state to Parent so both ColorPicker and ColorDisplay share the same color value. Changing the dropdown updates the displayed color.
import React, { useState } from 'react'; function Parent() { const [color, setColor] = useState('red'); return ( <div> <h2>Choose a color:</h2> <ColorPicker color={color} setColor={setColor} /> <ColorDisplay color={color} /> </div> ); } function ColorPicker({ color, setColor }) { return ( <select value={color} onChange={e => setColor(e.target.value)} aria-label="Select color"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select> ); } function ColorDisplay({ color }) { return <p>The selected color is <strong>{color}</strong>.</p>; } export default Parent;
Always lift state to the closest common parent of components that need it.
Passing state down as props keeps components in sync.
Too much lifting can make code complex; consider context or state libraries for bigger apps.
Lifting state moves shared data up to a parent component.
This allows multiple child components to read and update the same state.
It keeps data consistent and avoids duplication.
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
