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)withgetStateanddispatch, guarded the way Redux guards them;subscribewith snapshot semantics, so listeners can come and go during a dispatch;watchandshallowEqual, to react only to the slice of state you select;applyMiddleware,compose,thunkand a logger;useStore, a React hook onuseSyncExternalStorethat 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
- 01Create the storeWrite createStore: keep the state in a closure and change it only by dispatching actions to a reducer.
- 02Subscribe to changesLet code listen for state changes with subscribe, and keep notifications predictable while listeners come and go during a dispatch.
- 03Selectors and equalityWatch one slice of the state, and let an equality function decide whether it really changed.
- 04MiddlewareWrap dispatch in a chain of middleware, then write thunk for async logic and a logger.
- 05Connect ReactBuild useStore on useSyncExternalStore, so a component re-renders only when its selected slice changes.
- 06Run it: a todo appFinish the toggle feature of a small React todo app that runs on your own store, middleware and hook.
Practise this
Other hands-on work on the same topics.
Incident Simulator
System Design
Weekly Challenges
Frontend History
See it in real code
Guided tours through the source of real libraries that rely on it.