What makes an external-store subscription safe for React rendering?

React state & reliabilityTechnical deep diveAbout 11 minutes

Understand the idea, explain it in your own words and use the rubric to find your next learning gap.

Local drafts · no account or AI calls
Sukoaa original

The idea behind the question

An external store needs a subscription and a snapshot that changes identity when its data changes while remaining stable when nothing changes. A server snapshot must also support consistent initial hydration.

AN ILLUSTRATIVE EXAMPLE

A getSnapshot function returns a new object every time, causing repeated updates even though the store value has not changed.

TRY A SMALL EXAMPLEJavaScript · standalone helper
// Standalone store core; a React UI would subscribe with useSyncExternalStore.
function createStore() {
  let snapshot = Object.freeze({ count: 0 });
  const listeners = new Set();
  return {
    getSnapshot: () => snapshot,
    subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); },
    increment() { snapshot = Object.freeze({ count: snapshot.count + 1 }); for (const fn of listeners) fn(); }
  };
}
const store = createStore();
const before = store.getSnapshot();
console.log(before === store.getSnapshot());
let calls = 0;
const unsubscribe = store.subscribe(() => calls++);
store.increment();
unsubscribe();
store.increment();
console.log(before.count, store.getSnapshot().count, calls);
console.log(before === store.getSnapshot());
Show expected output
true
0 2 1
false
Open JavaScript playground

Copy and paste to run in a separate tab. This example illustrates one concept; it is not a complete application or an automatically graded task.

A practical way to learn it

Implement a tiny immutable store with getSnapshot and subscribe. Verify that unchanged reads share identity and that unsubscribe stops future notifications.

Open React project studio

The related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.

Build an explanation you understand

  1. State what the concept does in plain language.
  2. Apply it to the example and make your assumptions explicit.
  3. Explain a trade-off, failure case and how you would test it.
  4. Distinguish your actual experience from a proposed approach.

Further reading

Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.