How can you lift state up in React?

HighIntermediateReact
Preparing for interviews?

Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.

Quick Answer

Lifting state up means moving shared state to the closest common ancestor and passing it down via props. This keeps siblings in sync and avoids duplicated state. The trade-off is prop drilling vs shared state, so test updates from each child and consider performance and memoization.

Answer

Overview

In React, when two or more components need to share the same state, it’s best to 'lift the state up' — move it to their nearest common parent. The parent holds the state, and passes it down to children via props. This ensures data consistency and centralized control.

JSX
// Example: Lifting state up
function Parent() {
  const [value, setValue] = useState('');

  return (
    <div>
      <Input value={value} onChange={setValue} />
      <Display value={value} />
    </div>
  );
}

function Input({ value, onChange }) {
  return <input value={value} onChange={(e) => onChange(e.target.value)} />;
}

function Display({ value }) {
  return <p>Typed text: {value}</p>;
}
                  

Concept

Explanation

Centralized State

The parent manages state instead of each child maintaining its own.

Data Flow

State is passed down via props; updates flow upward via callback functions.

Synchronization

Ensures that multiple components always display the same data.

Core principles of lifting state up in React

Why Lift State

      • To share data between sibling components.
      • To prevent duplication of logic or inconsistent state.
      • To manage user input that affects multiple components.

Best Practices

      • Keep state as high as necessary, but as low as possible — avoid globalizing everything.
      • Use callbacks to propagate changes from children to parents.
      • Consider using Context API or state management libraries for complex shared state.

Think of lifting state up as giving one parent the responsibility of tracking data, while children simply read or update it.

Practical scenario
Two sibling components need to stay in sync (e.g., a slider and a numeric input), so you move shared state to their parent.

Common pitfalls

      • Excessive prop drilling as the tree grows.
      • Duplicating derived state in multiple places, causing inconsistencies.
      • Triggering unnecessary re-renders for unrelated children.
Trade-off or test tip
Lifting state improves consistency but can increase coupling. Test by updating each child and verifying the other reflects changes immediately.

Summary
      • Lifting state up moves shared data to a common ancestor.
      • Children communicate changes via callbacks.
      • It promotes consistency and unidirectional data flow in React.
Similar questions
Guides
1 / 41