What makes an external-store subscription safe for React rendering?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
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.
A getSnapshot function returns a new object every time, causing repeated updates even though the store value has not changed.
// 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
falseCopy 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 studioThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would use useSyncExternalStore for an actual external source rather than hand-build a render-time subscription. The subscription must return cleanup, and getSnapshot must return an immutable, cached value until the underlying data changes. I would avoid mutating a previously returned snapshot. For server rendering I would define a compatible server snapshot or keep the feature intentionally client-only. Tests should cover subscribe, unsubscribe, unchanged reads and changes between rendering and subscription. For state owned entirely by one component, regular React state is usually simpler than introducing a separate store.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Returning a fresh object from getSnapshot for every read.
Now go one level deeper
- How would you adapt a mutable third-party store?
- Why might localStorage alone need an additional same-tab notification?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.