← Back to React, page 2

react-0028 · Easy fresher

What does "lifting state up" mean, and when should you do it?

Lifting state up means moving state from a child to the nearest common parent of the components that need it, then passing the value down as props and the setter (or a callback) down for changes. Do it when two components must stay in sync, such as a search box and a results list. Keep state as low in the tree as possible and lift it only as far as needed.

Full answer

Data in React flows one way, from parent to child. Siblings cannot read each other's state directly. If a search box and a list of results must agree on the same query, neither can own it alone.

The solution is to move the state to their closest common parent. The parent holds query, passes it to the list as a prop, and passes the box a value and an onChange callback. When the user types, the box calls the callback. The parent updates its state, re-renders, and both children receive the new value. There is now exactly one source of truth.

The opposite problem is lifting too high. If state lives at the top of the app, every change re-renders a large part of the tree, and components become cluttered with props they only pass along. Keep state close to where it is used. Lift only when two or more components need it.

A related decision is whether a value is state at all. In the example, the filtered list is computed during render from the full list and the query. It is not stored separately, so it cannot be out of date.

When the chain of props gets long, alternatives exist: composition, context, or a state library. These are tools for the same problem, giving components access to shared data without passing it by hand.

Lifting state also makes components more reusable. A controlled search box that receives value and onChange can be used anywhere, because it does not own the data.

jsx
import { useState } from 'react';

const students = ['Asha', 'Ravi', 'Meera', 'Ashok'];

function SearchBox({ value, onChange }) {
  return <input value={value} onChange={(e) => onChange(e.target.value)} placeholder="Search students" />;
}

function ResultList({ query }) {
  const matches = students.filter((name) => name.toLowerCase().includes(query.toLowerCase()));
  return (
    <ul>
      {matches.map((name) => (
        <li key={name}>{name}</li>
      ))}
    </ul>
  );
}

export default function App() {
  const [query, setQuery] = useState('');
  return (
    <div>
      <SearchBox value={query} onChange={setQuery} />
      <ResultList query={query} />
    </div>
  );
}

The query lives in App. SearchBox reports changes through onChange, and ResultList reads the same query as a prop. The check typed Asha into the box. Rendered result: <div><input placeholder="Search students" value="Asha"><ul><li>Asha</li></ul></div>

Key points

  • Move state to the closest common parent of the components that need it.
  • Pass the value down as props and changes up through callback props.
  • Keep state as local as possible, and lift only when needed.
  • Compute derived data in render instead of storing a second copy.
  • Components that receive value and onChange are reusable and easy to test.

Common mistakes

  • Keeping a copy of the query in both children and trying to sync them with effects.
  • Lifting all state to the root of the app out of habit.
  • Storing the filtered list in state when it can be computed from the query.
  • Passing the setter down under many layers instead of considering composition or context.

Follow-ups

  • What are the downsides of lifting state too high?
  • How would you avoid re-rendering the whole list on every keystroke for a large data set?
  • When would you replace lifted state with context or a store?

Tips

  • Describe the idea in terms of a single source of truth.
  • Say "as low as possible, as high as necessary". It shows judgement.
  • Use a familiar example, such as a search box with a list.