Full answer
All state tools answer two questions: where does state live, and how do components find out that it changed.
Context. The value is held by a provider component. When the value changes, React re-renders all components that call useContext on it, because context has no selector. It is ideal for rarely changing values (theme, user, locale) and inconvenient for frequently changing state, unless you split contexts or memoize.
External store with selectors (Redux, Zustand). State lives outside React in a plain JavaScript object. Components subscribe through a hook such as useSelector(state => state.cart.count) or useStore(s => s.count). When the store changes, every subscriber runs its selector and compares the result with the previous one using Object.is (or a custom equality). Only components whose selected value changed re-render. Redux adds a strict pattern (actions, reducers, middleware, devtools). Zustand is a small store with functions in the state object and no provider requirement.
Atomic state (Jotai, and the idea behind Recoil). State is split into small atoms, and components subscribe to specific atoms. Derived atoms recompute when their inputs change. Updates are precise by construction.
Proxy-based and mutable styles (Valtio, MobX) track which properties each component reads and re-render only for those.
The React integration point is useSyncExternalStore(subscribe, getSnapshot). It subscribes, reads the current value during render, and keeps all components consistent under concurrent rendering (no tearing). The rules: getSnapshot must return a stable value when the state did not change, so selectors should return primitives or existing references, and not build new objects on each call.
Choosing: Context for slow, app-wide configuration. A store for frequently changing client state shared across distant components. A data library (TanStack Query, RTK Query) for server state. Local state for the rest.
The example builds a small store with selectors and shows that only the component reading the changed slice re-renders.
import { useSyncExternalStore } from 'react';
function createStore(initial) {
let state = initial;
const listeners = new Set();
return {
setState(update) {
state = { ...state, ...update(state) };
listeners.forEach((listener) => listener());
},
useStore(selector) {
return useSyncExternalStore(
(listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
() => selector(state)
);
},
};
}
const { setState, useStore } = createStore({ a: 0, b: 0 });
function CounterA() {
const a = useStore((state) => state.a);
console.log('CounterA rendered with ' + a);
return <button onClick={() => setState((s) => ({ a: s.a + 1 }))}>Add A ({a})</button>;
}
function CounterB() {
const b = useStore((state) => state.b);
console.log('CounterB rendered with ' + b);
return <button onClick={() => setState((s) => ({ b: s.b + 1 }))}>Add B ({b})</button>;
}
export default function App() {
return (
<div>
<CounterA />
<CounterB />
</div>
);
}The check pressed Add A and then Add B. Each press re-rendered only the component whose selected value changed. Output of the run: CounterA rendered with 0 CounterB rendered with 0 CounterA rendered with 1 CounterB rendered with 1
Key points
- Context re-renders all consumers on any value change and has no selectors.
- Redux and Zustand subscribe with selectors, so only components with a changed slice re-render.
- Atomic and proxy-based libraries track dependencies even more precisely.
- useSyncExternalStore connects external stores to React safely, with stable snapshots.
- Choose context for slow config, a store for shared client state, and a data library for server state.
Common mistakes
- Putting fast-changing state in a single context and re-rendering every consumer.
- Writing a selector that returns a new object or array every time.
- Selecting the whole state in a store hook.
- Mutating the state object inside the store.
- Choosing a global store for state that is local to one component.
Follow-ups
- Why must the getSnapshot function return a cached value?
- How do Jotai atoms differ from selectors on a single store?
- How would you add middleware or devtools support to this store?
Tips
- Compare the notification models, not only the APIs.
- Mention useSyncExternalStore as the common foundation.
- Warn about selectors that create new objects.