What you're building
A reactive state store — the primitive at the heart of Redux, Zustand, MobX, and Svelte's store contract. When state changes, all subscribers are notified automatically.
Requirements
Build a Store class that accepts an initial state object:
getState()— return the current state (a plain object). Return a shallow copy so callers cannot mutate internal state directly.setState(partial)— mergepartialinto current state (like React'ssetState). Notify all subscribers after the merge.subscribe(listener)— register alistener(newState, prevState)callback. Return an unsubscribe function.reset()— restore state to the original initial value and notify subscribers.select(selectorFn)— returnselectorFn(currentState)(derived value, no subscription).
Subscribers must receive copies, not references to internal state, so mutating the argument has no effect on the store.
Stretch goals
- Middleware support: accept an array of middleware functions that intercept
setStatecalls. - Computed/memoised selectors:
createSelectorthat only recomputes when relevant slice changes.
What this proves
You understand immutability, shallow merging, the observer pattern, and why frameworks like Redux enforce pure reducers. This is senior-level frontend/fullstack territory on any interview.
⚡ 60 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
getState returns initial values
setState merges and notifies
unsubscribe stops notifications
reset restores initial state
Run your code to check these.