useSyncExternalStore API and Parameters
Learn the subscribe, getSnapshot, and getServerSnapshot arguments and what they must guarantee.
useSyncExternalStore API and Parameters is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Arguments Overview
useSyncExternalStore takes up to three arguments: subscribe, getSnapshot, and an optional getServerSnapshot. Together they tell React how to listen for changes and how to read the current value.
Understanding each argument is essential, since small mistakes in them are the most common source of bugs when using this hook.
The subscribe Function
subscribe receives a callback from React and must register it with the store so the callback runs whenever the store changes. It must return an unsubscribe function that React calls on cleanup.
React uses this to know when to re-read the snapshot, so subscribe is purely about wiring up and tearing down the change notification.
getSnapshot Returns the Current Value
getSnapshot returns the current value of the store. React calls it during render to read state and again after notifications to detect whether the value changed.
Crucially, it must return a referentially stable result when the underlying data has not changed, returning the same reference rather than a fresh object each time.
getServerSnapshot for SSR
The optional getServerSnapshot provides the value used during server-side rendering and the initial hydration pass. It exists because the browser-only getSnapshot may rely on APIs that do not exist on the server.
Providing it prevents hydration mismatches by giving the server a sensible value to render that matches what the client will produce initially.
The Return Value
The hook returns the current snapshot value, which you use directly in your component like any other piece of state. When the store changes, React re-renders and the hook returns the updated snapshot.
From the component perspective it feels like reading reactive state, even though the source of truth lives entirely outside React.
getSnapshot Must Be Synchronous and Consistent
getSnapshot must run synchronously and return a value derived purely from the current store state. It should not perform async work or produce different results for the same underlying data.
React relies on calling it repeatedly and comparing results, so any non-determinism or side effects inside it can cause incorrect behavior.
The Immutable Snapshot Pattern
Stores that hold immutable state make getSnapshot easy, since the current state object reference changes only when the data changes. Returning that reference gives stable comparisons for free.
This is why immutable update patterns pair so well with the hook: equal data means an equal reference, and React skips needless re-renders.
The Infinite Loop Mistake
A frequent error is returning a brand new object or array from getSnapshot on every call, for example mapping or filtering inside it. React sees a new reference each time and thinks the value changed.
That triggers endless re-renders or warnings. The fix is to cache the derived value and return the same reference until the source data actually changes.
Keeping Snapshots Stable
To avoid the loop, compute derived data outside getSnapshot or memoize it so the function returns a cached reference. The selector-style pattern many libraries use exists precisely to manage this stability.
The guiding rule is that two calls to getSnapshot with unchanged store data must return the exact same reference.
Putting the Parameters Together
In practice you write a subscribe that adds and removes a listener, a getSnapshot that reads a stable current value, and, for SSR, a getServerSnapshot returning a server-safe value.
With those three correct, the hook delivers consistent, concurrent-safe reads of your external store with minimal ceremony.
Quick Check: useSyncExternalStore Parameters
Identify the role of getSnapshot.
Recap: useSyncExternalStore API
The hook takes subscribe, getSnapshot, and optionally getServerSnapshot. subscribe wires a listener and returns an unsubscribe; getSnapshot reads the current value synchronously and must be referentially stable; getServerSnapshot supplies an SSR-safe value.
The classic pitfall is returning a new object every call, which causes infinite loops. Cache derived values so unchanged data yields the same reference.
Frequently asked questions
Is the “useSyncExternalStore API and Parameters” lesson free?
Yes — the full text of “useSyncExternalStore API and Parameters” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “useSyncExternalStore API and Parameters”?
Learn the subscribe, getSnapshot, and getServerSnapshot arguments and what they must guarantee. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “useSyncExternalStore API and Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Problem with External Store Subscriptions
- useSyncExternalStore API and Parameters
- Subscribing to Browser APIs
- Building a Custom Store with useSyncExternalStore