Back to results for “Context API”
React › State Management

How do you decide between using Context, a state management library, or simply lifting state up?

Advanced

Answer

Lifting state up works well when only a few closely related components need to share data. Context works well for moderately shared data that does not change extremely often, like a theme or the current user. A dedicated state management library becomes useful when an app has a lot of shared, frequently changing data, or when performance concerns make Context's re render behavior a real problem.
// Simple case, lifting state up is enough
function Parent() {
  const [value, setValue] = useState('');
  return <><Input value={value} onChange={setValue} /><Display value={value} /></>;
}

// Complex, frequently changing global data, a library like Zustand may fit better
Real-world example A small team app manages a shared color theme using Context, since it rarely changes, but switches to Zustand for their frequently updating real time notification data, since Context's re render behavior started causing performance issues there.

Common follow-ups: What specific performance problem can happen when Context is used for very frequently changing data?;How would you migrate from using Context to a dedicated state management library later if needed?