React’s useSyncExternalStore is a low-level React hook introduced in React 18 to support safely reading from external sources of state with concurrent rendering modes, such as React’s Concurrent Mode.
This hook is especially important in the context of external stores/libraries (like Redux, Zustand, etc.) that manage their own state outside React’s rendering system, and which need to integrate properly with React’s concurrent rendering and reactivity model.
✳️ Summary
const state = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot?)
Parameters:
-
subscribe: A function that lets React subscribe to changes in the external store.
(callback: () => void) => () => void
-
getSnapshot: A function that returns the current value from the store.
() => T
-
getServerSnapshot (optional): Used in server-side rendering (SSR) for hydrating external store values.
() => T
🔍 Why Was useSyncExternalStore Introduced?
Before its introduction, developers often used combinations like useEffect, useState, or custom hooks (e.g. useStore) to read from external stores. However, these didn’t play well with Concurrent Rendering because:
- Outdated renders: React could re-render based on locked-in snapshot values that no longer reflect the actual store.
- Tearing: React might read inconsistent data from the store during a render phase.
useSyncExternalStore solves this by:
- Guaranteeing consistency: It ensures that the component will re-render synchronously after the store updates.
- Stabilizing snapshot values: React reads from the store before rendering so the snapshot is always fresh.
- Preventing tearing during concurrent renders.
🔁 Comparison with Older Patterns
Example – Using useEffect and state:
function MyComponent() {
const [count, setCount] = useState(store.getCount());
useEffect(() => {
const unsubscribe = store.subscribe(() => {
setCount(store.getCount());
});
return unsubscribe;
}, []);
return <div>{count}</div>;
}
⚠️ This approach:
- Doesn’t work reliably in concurrent mode
- May show stale values due to asynchronous effects
✅ With useSyncExternalStore
import { useSyncExternalStore } from 'react';
function useCount() {
return useSyncExternalStore(
store.subscribe, // Subscribes to the store
store.getCount, // Gets the current value
store.getServerSnapshot // (optional) SSR support
);
}
function MyComponent() {
const count = useCount();
return <div>{count}</div>;
}
Benefits:
- Reactive and consistent
- Safe in Concurrent Rendering
- Hydration-compliant (with
getServerSnapshot)
🧪 How It Works Internally
- At initial render, React calls
getSnapshot() to get the current value.
- React subscribes to the store using
subscribe(callback). The callback will be called on store changes.
- If the return value of
getSnapshot() changes, React schedules a re-render.
- During re-render, React again calls
getSnapshot() to get the latest data.
- Before committing, React compares the new snapshot to the previous one to avoid unnecessary updates.
Tearing Prevention:
Because React always reads the snapshot during render and not in a useEffect, it prevents situations where a stale snapshot is "locked in" before data updates.
✅ When to Use It
Use useSyncExternalStore whenever reading from external, non-React-managed state that can change independently of React.
Examples:
- Global state managers like Redux, MobX, Zustand, recoil (some of them internally use this hook)
- Subscriptions to browser APIs (e.g.
location, media queries)
- Legacy state containers or Flux patterns
🛠️ Example with a Custom Store
// counterStore.ts
let count = 0;
let listeners: (() => void)[] = [];
export const counterStore = {
getSnapshot: () => count,
subscribe: (callback: () => void) => {
listeners.push(callback);
return () => {
listeners = listeners.filter(l => l !== callback);
};
},
increment: () => {
count++;
listeners.forEach(fn => fn());
},
};
function useCounter() {
return useSyncExternalStore(
counterStore.subscribe,
counterStore.getSnapshot
);
}
function Counter() {
const count = useCounter();
return (
<div>
<p>Count: {count}</p>
<button onClick={() => counterStore.increment()}>Increment</button>
</div>
);
}
⚙️ SSR Support: getServerSnapshot
React will call getServerSnapshot() on the server to get the initial state. This helps prevent mismatches between server and client renders.
const snapshot = useSyncExternalStore(
store.subscribe,
store.getSnapshot,
store.getServerSnapshot // only used during SSR
);
If no server snapshot is provided and hydration fails, React shows a warning or even throws hydration errors.
📎 Redux Example (React-Redux v8+)
React-Redux v8+ internally uses useSyncExternalStore to create useSelector.
You don’t usually need to use this hook directly with Redux anymore — use useSelector. But you can if you want full control or build custom connectors.
🧠 Key Takeaways
| Feature | Description |
|---|
| Ensures consistency | Reads value before rendering, updates synchronously |
| Prevents tearing | Renders based on latest snapshot |
| Works with concurrent mode | Safe for future React features |
| Enables SSR hydration | Supports getServerSnapshot() |
| Intended for external stores | Not for internal React state |
🛑 Things to Avoid
- Don't mutate store state inside
getSnapshot() – it should be a pure function.
- Avoid using this for regular React state – it's intended specifically for external state.
- Don’t wrap this in
useMemo or useCallback unnecessarily – React tracks dependencies correctly on its own.
🔚 Conclusion
useSyncExternalStore is React's low-level primitive for external state binding designed for safety, consistency, and performance in modern React (including concurrent futures and SSR).
It gives external state libraries a consistent and safe way to synchronize with React's rendering model.
If you’re building a custom state management solution or integrating React with a non-React state system — useSyncExternalStore is your friend.
Let me know if you want an example using Redux, Zustand, or a browser API (like geolocation, matchMedia, etc.).