Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Minimal Redux-style Store

Hooks Logichard

Course · Section 10: A Closer Look at Functions · Lecture 144: Closures

Build a tiny store. Implement `createStore(reducer, initialState)` returning an object with: - `getState()` returns the current state. - `dispatch(action)` runs the reducer and notifies all subscribers. - `subscribe(listener)` registers a listener called on every dispatch.

Examples

Input: inc reducer; subscribe a counter; dispatch inc, inc, noop
Output: [2, 3]
State ends at 2; the subscriber fired on all three dispatches.

Sample tests

Input: dispatch notifies subscribers
Output: [2,3]
Input: state updates via reducer
Output: 15

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Notifying subscribers before updating state.
  • Not capturing state privately in the closure.

Learning resources

  • React: Scaling Up with Reducer and Context
Approach & explanation (try first)

A closure holds private state and listeners; dispatch threads the reducer and notifies subscribers, the core of Redux.

Loading...
⌘/Ctrl + Enter

Run your code to see results.