What is Lifting State Up in React?
Learn what lifting state up means in React: move shared state to a common parent for one source of truth, pass props down, with a clear code example.
Expected Interview Answer
Lifting state up is the React pattern of moving shared state to the closest common parent of the components that need it, so a single source of truth flows down as props.
When two or more sibling components must stay in sync, you remove the state from each child and place it in their nearest shared ancestor. The parent holds the state and passes both the value and an updater callback down as props. Children read the value from props and call the callback to request changes, keeping all related components consistent through one authoritative copy of the data.
- Creates a single source of truth for shared data
- Keeps sibling components synchronised
- Makes data flow predictable and top-down
- Simplifies debugging by centralising state
- Avoids duplicated, drifting copies of state
AI Mentor Explanation
Imagine two scoreboards at opposite ends of the ground each kept by a different helper — they soon disagree. The fix is to have one central scorer own the score and feed both boards. Lifting state up does this: shared state moves to a common parent that owns it, and children display the same single value.
Step-by-Step Explanation
Step 1
Spot the shared data
Identify state that two or more components need to read or update together.
Step 2
Find the common parent
Locate the closest ancestor that renders all the components needing the state.
Step 3
Move state upward
Remove the state from the children and declare it in that common parent with useState.
Step 4
Pass value and callback down
Send the state value and an updater function to children through props.
Step 5
Update via callbacks
Have children call the passed-in callback to change state, so the parent stays the single source of truth.
What Interviewer Expects
- Understanding single source of truth
- Knowing React's one-way top-down data flow
- Moving state to the closest common ancestor
- Passing value and updater callbacks as props
- Recognising when to lift versus use Context
Common Mistakes
- Duplicating state in multiple children instead of lifting it
- Lifting state higher than necessary, causing needless re-renders
- Forgetting to pass an updater callback so children cannot change state
- Confusing lifting state up with prop drilling as the only option
- Keeping conflicting copies of the same data in siblings
Best Answer (HR Friendly)
“Lifting state up means moving shared information to the nearest parent component so everyone reads from one place. The parent owns the data and passes it down, which keeps related parts of the screen in sync and easier to manage.”
Code Example
import { useState } from 'react'
function TemperatureInput({ label, value, onChange }) {
return (
<label>
{label}:
<input value={value} onChange={(e) => onChange(e.target.value)} />
</label>
)
}
export default function Calculator() {
const [temperature, setTemperature] = useState('')
return (
<div>
<TemperatureInput label="Celsius" value={temperature} onChange={setTemperature} />
<TemperatureInput label="Mirror" value={temperature} onChange={setTemperature} />
<p>Both inputs share one value: {temperature}</p>
</div>
)
}Follow-up Questions
- How does lifting state up relate to a single source of truth?
- What is prop drilling and how does it connect to lifting state?
- When should you use Context instead of lifting state up?
- Why does React use one-way (top-down) data flow?
- How do children request state changes from a parent?
MCQ Practice
1. Where does state go when you lift it up?
Lifting state up moves shared state to the nearest common ancestor of the components that need it.
2. How do child components change lifted state?
The parent passes down an updater callback; children call it to request state changes while the parent stays the source of truth.
3. What key benefit does lifting state up provide?
Centralising shared state in one parent creates a single source of truth, keeping dependent components in sync.
Flash Cards
What is lifting state up? — Moving shared state to the closest common parent so it is the single source of truth passed down as props.
How do children update lifted state? — By calling an updater callback the parent passes down as a prop.
Why lift state up? — To keep sibling components in sync and avoid duplicated, drifting copies of the same data.
Lift vs Context? — Lift for a few nearby components; use Context when many deeply nested components need the same data.