Lifting State Up — React

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…

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>
  )
}
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>
  )
}

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:

  1. Find which components need that data.
  2. Find the closest parent that wraps all of them.
  3. Move the useState to that parent.
  4. Pass the value down through props (value, name).
  5. 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

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.

What you will learn