· By John Kavanagh

Keeping React External Store Snapshots Consistent

Photograph of a building reflected across glass panels.
Image by Robert Keane, licensed from Unsplash.

The badge says there's one handover item. The list has one too. The store has two, which is awkward because the two things on screen agree with each other quite convincingly.

I made that happen with a small fictional handover board. One React component counts the cards, and another lists them. Both read the same store, which accepts updates outside React. The example starts with one card and adds another after the components have read the first revision, just before their first subscription goes in. There's no slow request to blame. The order is enough.

If the badge and list are the only places that need the value, I'd lift that state to a shared parent. The handover board has a different constraint. Another part of the application can write to its store, and both React consumers need to see those writes without keeping their own copies in step.


The Write Nobody Heard

The first version reads the store when the component renders and keeps a copy of that value in React state. It signs up for future writes in an effect.

const [snapshot, setSnapshot] = useState(getSnapshot);

useEffect(
  () => subscribe(() => setSnapshot(getSnapshot())),
  [getSnapshot, subscribe],
);

The effect even returns the unsubscribe function. The problem sits before that cleanup has anything to clean up. Both widgets commit revision 10. The store publishes revision 11 with zero listeners, then the effect registers its listener. The listener is in the right place for the next write, but revision 11 has already gone by.

Run the effect version and both widgets stay at revision 10, with one item apiece. Another write brings them straight to revision 12. Testing only that second update makes this version look fine.

The same board with useSyncExternalStore gets the same revision 11 write before the first listener is installed. In that version, both widgets advance to revision 11 without a second write. I put the write into the subscription setup deliberately, so the result depends on a known order of events rather than on how quickly the machine runs.


Keep a Snapshot until Something Changes

The new external‑store hook in React 18 takes a function that subscribes and a function that reads the current snapshot. The subscription tells React to check the store again. The snapshot tells it whether the value has changed, using Object.is to compare successive reads. The third argument supplies the initial value for server rendering and hydration.

const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);

I keep the current snapshot in the store. A read just returns it. An equal write leaves that object alone. A real write creates a new immutable revision, installs it as the current value, and then calls the listeners. The store copies and freezes the little card records as well as the outer snapshot. Keeping the outer object fixed whilst mutating one of its cards would leave React with an unchanged identity and quietly change what its older reads meant.

The opposite mistake is easy to make in a hurry. Returning { ...current } from the getter looks like a tidy way to protect the stored object. Now two consecutive reads disagree by identity even when nobody has written anything. The lab compares those reads directly alongside the cached getter. It's a quick way to see the broken assumption before mounting a reader that turns it into a render loop.

I keep subscribe stable for a given store too. React resubscribes when that function changes. Let the returned function remove the exact registration it created. If both readers need the same adapter, extract it into a custom hook and keep the store's subscribe function stable when you call it.


Hydrate from the Request That Produced the Page

With server‑side rendering, the server can put useful data straight into the HTML. The browser may already have newer data when hydrateRoot runs. The first client render still has to describe the HTML it is attaching to. If those snapshots disagree, collect the server response and the first client state as separate pieces of evidence.

My server creates a new board seed for each request and includes it as JSON in the document. On the client I parse that seed once, keep it available, and start the live store from a copy. The getServerSnapshot callback returns the seed. The getSnapshot callback can then read whichever revision the browser store has reached. The data must agree across server and browser. The JavaScript objects live in separate processes.

In the north board example, the live store advances to revision 11 before calling hydrateRoot. At the first commit, the badge element from the server is still in place and both widgets show revision 10. They then move to revision 11. The south board repeats the check with its own data at revisions 40 and 41. Neither route reports a recoverable hydration error.

In the wrong‑seed example, the hydration callback reads the live store. React reports a recoverable mismatch and replaces the north board's server badge. React's upgrade guide shows how to switch to hydrateRoot. Keep request‑owned data out of a mutable server singleton, and only serialise values the browser is allowed to see. The lab escapes < before putting the JSON in its script element. If the store has no meaningful server value, mount the reader on the client. Calling the hook during server rendering without getServerSnapshot throws.


Check the Two Readers, Then Take Them Away

The lab uses React and React DOM 18.1.0 on Node.js 16.15.0. The browser tests read both committed widget revisions and compare the badge count with the actual list length. Unmounting takes the listener count from two to zero. Remounting restores two subscriptions and reads the current board. There's a separate check for the final data and cleanup with development Strict Mode.

One case holds a preview open on a promise whilst a store write goes through. Both widgets show the new revision with the preview still pending. The promise is a test fixture that lets you hold the preview open yourself. The React 18 working group explains the separate rule for store updates. Wrapping an external store write in startTransition won't stop the hook handling that write synchronously.

If you're upgrading an app that already uses a store library, check what its React 18 binding does before writing your own. When I test an adapter like this, I want to know who published the value and which revision each reader committed. Then I close the board and check who still has a listener.

Postscript

November 2026: React 19.2 has since added useEffectEvent. It lets an Effect callback read the latest props and state without making those values trigger the Effect again. That helps with stale closures, but it won't recover a store update that happened before the listener existed. I'd still use useSyncExternalStore for this board. The two widgets need a consistent store snapshot, not just a callback with a better memory.

Downloadable Resource

  • React External Store Snapshot Lab

    Compare a stale Effect subscription with a consistent external‑store snapshot, then test two consumers and server hydration.

    Download ZIP

Looking for technical direction?

I support teams that need senior judgement on React, Next.js, headless CMS architecture, performance, migrations, and technical SEO.