How it works · state management for React

How Zustand works

A store is a value, a way to change it, and a list of who wants to know. The whole core is about a hundred lines.

Think of it like this. A noticeboard: one sheet of paper, anyone can replace it, and everyone who signed up gets a tap on the shoulder when it changes.

  1. The store Holds the state, changes it, and calls everyone who subscribed. src/vanilla.ts
  2. The React hook Lets a component read one piece of the store and redraw when that piece changes. src/react.ts
  3. Middleware Optional wrappers around a store: save it, log it, edit it with Immer. src/middleware
  4. Persist The biggest wrapper: saves the state to storage and loads it back. src/middleware/persist.ts

Follow one thing through: set({ count: 1 })

  1. If you passed a function, it is called with the current state to get the change.src/vanilla.ts
  2. If the result is the very same value as before, nothing happens.src/vanilla.ts
  3. Otherwise the change is merged into a copy of the state. The old object is left alone.src/vanilla.ts
  4. Every listener is called with the new state and the old one.src/vanilla.ts
  5. In React, each component runs its selector on the new state, and redraws only if its piece changed.src/react.ts

The clever bit: React already had the hard part

The React side is one call: useSyncExternalStore(api.subscribe, () => selector(api.getState())). React asks “has the thing you selected changed?” after every update, and redraws the component only when it has. So a component that reads state.count ignores every other change in the store, and Zustand needs no provider, no context and no list of dependencies.

src/react.ts

Words you’ll meet

store
The one object that holds the state and the ways to change it.
selector
A function that picks the piece of state a component cares about.
subscribe
Ask to be called whenever the state changes.
middleware
A function that wraps a store to add a feature.

Good questions to ask next

Open Zustand in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take Zustand apart yourself.

Free and open source. Nothing to install, no account.

More projects, taken apart

How React worksYou describe what the screen should look like. React works out the smallest set of changes to make it so.How Express worksA request comes in and passes through a list of functions, in order, until one of them answers.How Flask worksYou write a function for each address. Flask finds the right one for a request, calls it, and turns what it…How Redis worksOne big dictionary held in memory, answered by a single loop that takes one command at a time.How nanoGPT worksText becomes numbers, a model learns to guess the next number, and guessing again and again writes new text.How micrograd worksEvery number remembers how it was made, so the answer can be traced back to say how much each input mattered.How SQLite worksYour SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.How FastAPI worksYou write a function with type hints. FastAPI reads the hints to check the request, convert the values and…How Vue worksVue notices which data each part of the screen reads, so when that data changes it knows exactly what to…How Requests worksOne friendly line, requests.get(url), on top of the fiddly work of opening connections and speaking HTTP.How Redux worksAll of an app’s state lives in one place, and the only way to change it is to describe what happened.How Git worksEvery version of every file is stored under a name made from its own contents, and a commit is a small note…How Preact worksThe same idea as React, in a few kilobytes: you describe the screen, and Preact changes only what is…How Axios worksOne way to make web requests that works the same in a browser and in Node, with hooks before and after every…How htmx worksAny element can ask the server for a piece of HTML and put it into the page. You write attributes, not…How LevelDB worksA store for keys and values that makes writing fast by never changing a file: it only adds, and tidies up…How etcd’s Raft worksSeveral servers keep the same list of changes in the same order, even when some of them crash. This is the…

Written by hand from the code as it was on 2026-12-02 (commit e76346b). Zustand belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.