Two cashiers in one small shop each write down the stock in their own notebook. Cashier A sells the last loaf of bread, but cashier B's notebook still says there's one left. The next customer is promised bread that's already gone.
React components can have the same problem. Sometimes two sibling components (children of the same parent) have to show the same data. If each one keeps its own state, they'll never agree. The fix: move that state to the closest parent that wraps both of them, then pass it down to the children through props. This technique is called lifting state up. It's like a well-run shop: one stock book on the middle table, and both cashiers read and write in that same book. In React, the stock book is the state, the two cashiers are the sibling components, and the middle table is their parent.
Two siblings that don't agree
In this example, NameInput and Greeting each keep name in their own state:
function NameInput() {
const [name, setName] = useState('Guest')
return <input value={name} onChange={(e) => setName(e.target.value)} />
}
function Greeting() {
const [name] = useState('Guest') // Greeting's own state
return <p>Hello, {name}!</p>
}
function App() {
return (
<div>
<NameInput />
<Greeting />
</div>
)
}
You type "Rina". The name state that belongs to NameInput becomes 'Rina', but Greeting has its own state, which stays 'Guest'. The result is "Hello, Guest!". In React, data only flows one way, from parent to child through props. This is called one-way data flow. A component can't read its sibling's state, so Greeting never learns that something new was typed.
Lift the state to the closest parent
This is the fixed version. The name state moves to App, and both children get it through props:
function NameInput({ name, onNameChange }) {
return <input value={name} onChange={(e) => onNameChange(e.target.value)} />
}
function Greeting({ name }) {
return <p>Hello, {name}!</p>
}
function App() {
const [name, setName] = useState('Guest') // the only place name is kept
return (
<div>
<NameInput name={name} onNameChange={setName} />
<Greeting name={name} />
</div>
)
}
- The
namestate now lives only inApp, the closest parent that wraps both children. One piece of data, one place. The term for it is single source of truth. - Data goes down through props:
name={name}is sent to both. A child only reads props, it doesn't change them. - Changes go up through a function:
ApppassesonNameChange={setName}, andNameInputcallsonNameChange(e.target.value)every time someone types in its field. Theonprefix is just a naming habit for props that hold a function.
| Step | What happens |
|---|---|
| 1 | You type until the field says "Rina", and onChange in NameInput runs |
| 2 | NameInput calls onNameChange('Rina'), which is App's setName |
| 3 | The name state in App becomes 'Rina', and App re-renders |
| 4 | App sends the new name to NameInput and Greeting, and both re-render |
| 5 | Greeting shows "Hello, Rina!" |
The same pattern for a number
This counter is split into two components: Display shows the number, and Controls holds the button:
function Display({ value }) {
return <span id="display">{value}</span>
}
function Controls({ onIncrement }) {
return <button id="add" onClick={onIncrement}>+1</button>
}
function App() {
const [count, setCount] = useState(0)
return (
<div>
<Display value={count} />
<Controls onIncrement={() => setCount((c) => c + 1)} />
</div>
)
}
Displayonly shows itsvalueprop.Controlsonly has a button that calls itsonIncrementprop. Neither keeps the number.countlives inApp, starting at 0. Click+1once and#displaybecomes 1. Click twice and it's 2.() => setCount((c) => c + 1)is a function passed toControls. It doesn't run during render, but later, when the button is clicked. The updater(c) => c + 1adds to the latest value, so two quick clicks still make 2.
When and how to lift
Lift state when two or more components need the same data. If only one component uses it, keep the state local to that component. The steps:
- Find which components need that data.
- Find the closest parent that wraps all of them.
- Move the
useStateto that parent. - Pass the value down through props (
value,name). - Pass a function to change it down through props (
onIncrement,onNameChange).
⚠️ Lifting gone wrong: state that is out of sync, or goes up too far
- Copying props into a child's state.
const [name, setName] = useState(props.name)in a child usesprops.nameonly as the starting value, on the first render. After that, when the parent sends a newname, the child's state doesn't change. Use the prop directly. - A child changing its own props.
props.name = 'Rina'doesn't trigger a render. In development, that line even throws aTypeError, because React freezes props (makes them impossible to change). A child asks for a change by calling the function from its parent. - Calling the function while passing it.
onIncrement={setCount(count + 1)}runs right away during render, then triggers another render, forever. Pass the function:onIncrement={() => setCount((c) => c + 1)}. - Lifting too high. State used by two children in one small part of the page doesn't need to go all the way to the top component. The higher the state goes, the more components in between only pass props down. This is called prop drilling. Lift only as far as needed, to the closest parent.
TL;DR: When two components need the same data, lift its state to the closest parent that wraps both. The technique is called lifting state up. The parent keeps the state with useState, passes the value down through props (value={count}, name={name}), and passes down a function to change it (onIncrement, onNameChange). The child calls that function, for example onNameChange(e.target.value). The parent then replaces the state, and every child re-renders with the same new value. Data flows one way, from parent to child. State that only one component uses stays local, kept in that component.
⚡ Two siblings that don't agree
Guess first: NameInput holds an input field, and Greeting shows Hello, {name}!. Both are children of the same parent, and each has its own name state with the starting value 'Guest'. You type "Rina" in the field. What does the greeting say?
Still "Hello, Guest!". Greeting's state is separate, so it doesn't change. A component can't read its sibling's state.
Meaning: In React, data only flows one way, from parent to child through props. This is called one-way data flow.
⚡ Lift the state to the closest parent
Write:
function App() {
const [name, setName] = useState('Guest') // the only place name is kept
return (
<div>
<NameInput name={name} onNameChange={setName} />
<Greeting name={name} />
</div>
)
}
Meaning: The name state now lives only in App, the closest parent that wraps NameInput and Greeting. Moving state to the closest parent is called lifting state up. Data goes down through props (name={name}). Changes go up through a function: NameInput calls onNameChange, which is setName.
⚡ The same pattern for numbers
Write:
<Display value={count} />
<Controls onIncrement={() => setCount((c) => c + 1)} />
Meaning: count is state that belongs to App. Display only shows its value prop, and Controls only has a button that calls its onIncrement prop. Keeping and changing count is still App's job.