Skip to content
FrontDepth, Home

Mini State Library

MiddleSenior

Build a Redux-style store step by step: reducers, subscriptions, selectors, middleware and a React hook on useSyncExternalStore.

What you will build

You will write a small state library from scratch in plain JavaScript, then run a React app on it:

  • createStore(reducer) with getState and dispatch, guarded the way Redux guards them;
  • subscribe with snapshot semantics, so listeners can come and go during a dispatch;
  • watch and shallowEqual, to react only to the slice of state you select;
  • applyMiddleware, compose, thunk and a logger;
  • useStore, a React hook on useSyncExternalStore that re-renders a component only when its selection changes.

After each step you compare your code with what Redux, React-Redux, Zustand and React itself do in their source.

What you will learn

  • Why a store changes only through dispatch, and how a closure keeps its state private
  • How a snapshot of listeners keeps notifications predictable while subscriptions change
  • How selectors and equality functions (Object.is, shallowEqual) decide what counts as a change
  • How middleware composes around dispatch, and why the order of thunk and logger matters
  • How useSyncExternalStore works, and why getSnapshot must return a cached value
  • Why immutable updates let React re-render only the component whose data changed

Steps

Other hands-on work on the same topics.

Guided tours through the source of real libraries that rely on it.