Integrating External State with React useSyncExternalStore
Connect React to mutable stores and browser APIs with stable snapshots, explicit subscriptions, and hydration-aware server data.
useSyncExternalStore is React’s integration point for data that changes outside React, such as a third-party store or browser API. It subscribes a component to changes and reads a snapshot that must remain stable while the underlying value has not changed.
THE PRODUCTION PROBLEM STATEMENT
Reading mutable external state during render does not tell React when that value changes. A hand-built effect subscription can also briefly render stale data or create inconsistent snapshots during concurrent rendering.
Expose a stable snapshot and a cleanup function
The subscribe function registers React’s callback and returns an unsubscribe function. The snapshot function reads the current store value. If the value has not changed, repeated reads must return an Object.is-equal result; returning a fresh object each time can create a re-render loop.
For an immutable store, the current state object can be a snapshot. For a mutable store, cache an immutable snapshot and replace it only when the store meaningfully changes.
type Snapshot = { count: number };
let snapshot: Snapshot = { count: 0 };
const listeners = new Set<() => void>();
export const counterStore = {
subscribe(listener: () => void) {
listeners.add(listener);
return () => listeners.delete(listener);
},
getSnapshot() { return snapshot; },
increment() {
snapshot = { count: snapshot.count + 1 };
listeners.forEach((listener) => listener());
},
};Use the hook for external data, not ordinary component state
React recommends built-in state when data belongs to the component tree. useSyncExternalStore is most useful when an external system owns the value and React needs to subscribe, including a browser signal such as online status.
Keep the subscribe function stable. If it is recreated on every render, React must unsubscribe and resubscribe. Define it outside the component when it needs no props, or memoize it when its identity depends on a specific input.
import { useSyncExternalStore } from 'react';
import { counterStore } from './counterStore';
export function Counter() {
const state = useSyncExternalStore(
counterStore.subscribe, counterStore.getSnapshot
);
return <button onClick={counterStore.increment}>{state.count}</button>;
}Provide a consistent initial snapshot for server rendering
When server rendering, pass getServerSnapshot if the external store has a meaningful initial value. The value returned on the server must match the initial client value during hydration, or the first render can disagree with the HTML.
Serialize request-specific initial state with the page. Avoid browser-only globals during server rendering and make store initialization predictable for each request.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. Use useState or useReducer for state owned by the component tree. Use this hook to subscribe to an external source.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
React Context vs Atomic Stores: Why Context Is Not a State Management Tool
Understanding dependency injection versus state orchestration, context re-render cascades, and when to adopt external selector-based stores.
Understanding React 18 Concurrency: useTransition, useDeferredValue, and Suspense Architecture
A deep architectural dive into cooperative scheduling, non-blocking state updates, fiber priority levels, and avoiding UI tearing in production React applications.