# pmndrs/zustand architecture

> Architecture diagram of the GitHub repository pmndrs/zustand, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pmndrs/zustand
- Repository: https://github.com/pmndrs/zustand

## Overview

1. This repository is a publishable state-management library, not an application. The product surface is the `zustand` package, with documentation, examples, tests, and release automation surrounding a small core runtime.

2. The architectural center is the store engine in `src/vanilla.ts`. It implements the framework-agnostic store primitives: state creation, synchronous updates, subscriptions, initial-state access, and non-React usage. This is the lowest-level boundary in the repo.

3. `src/react.ts` is the React adapter layer. It binds the vanilla store to React hooks so components can read slices of state and re-render only when selected data changes. This is where the “store is a hook” API exposed in the README is assembled.

4. `src/index.ts`, `src/traditional.ts`, `src/shallow.ts`, and the subfolders under `src/react` and `src/vanilla` define the public entrypoints and compatibility shims. These separate modern hook-based usage, traditional equality-function variants, and shallow-selection helpers into distinct import paths.

5. Middleware lives in `src/middleware/` and is the main extension boundary. It wraps store creation to add behavior without changing the core engine: `persist` for storage-backed state, `devtools` for Redux DevTools integration, `immer` for mutable-style updates, `redux` for reducer/dispatch semantics, `combine` for store composition, `subscribeWithSelector` for selector-based subscriptions, and `ssrSafe` for server-safe behavior.

6. The data flow is intentionally one-way and action-driven: app code calls store actions or `set`, the store updates internal state, and subscribers are notified synchronously. React components consume selected slices through hooks, while non-React code can use `getState`, `setState`, and `subscribe` directly from the vanilla store.

7. External integrations are narrow and explicit. `persist` reads/writes browser storage such as `localStorage` or `sessionStorage`; `devtools` talks to the Redux DevTools browser extension; `immer` depends on Immer’s `produce` semantics; and the SSR-related guides/middleware reflect the library’s need to behave correctly across client, server, and mixed renderers.

8. Selector and equality behavior is a first-class architectural concern. The repo includes shallow comparison helpers in `src/shallow.ts` and `src/react/shallow.ts`, plus tests around selector subscriptions. This supports fine-grained rendering and avoids unnecessary updates when derived values are structurally stable.

9. TypeScript support is built into the package surface rather than layered on top. `src/types.d.ts` and the TypeScript-focused docs and tests indicate that generics, middleware composition, and equality-function variants are all part of the supported API contract.

10. The repo includes two example applications that demonstrate integration patterns: `examples/demo` is a full Vite-based showcase with interactive UI and code previews, while `examples/starter` is a minimal starter template. These are documentation assets, not core runtime dependencies.

11. Verification is split across React, vanilla, middleware, SSR, subscription, shallow-equality, and type tests under `tests/`. That layout mirrors the main architectural boundaries and confirms that the library’s behavior is defined across multiple consumption modes, not just one framework path.

12. Build and release tooling is repo-level and package-oriented. `rollup.config.mjs`, `vitest.config.mts`, pnpm workspace files, and GitHub Actions workflows handle bundling, test matrices, docs publishing, preview releases, and size checks. This supports a small core package shipped in multiple formats with strict compatibility requirements.

Main entry points: [`src/middleware.ts`](https://github.com/pmndrs/zustand/blob/main/src/middleware.ts) (Middleware index), [`src/vanilla.ts`](https://github.com/pmndrs/zustand/blob/main/src/vanilla.ts) (Store engine), [`src/index.ts`](https://github.com/pmndrs/zustand/blob/main/src/index.ts) (Main entry), [`src/react.ts`](https://github.com/pmndrs/zustand/blob/main/src/react.ts) (React hook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_src_vanilla["Store engine<br/>store core<br/>[vanilla.ts]"]
  node_vanilla_shallow["Vanilla shallow<br/>selector helper<br/>[shallow.ts]"]
end

subgraph group_react["React layer"]
  node_src_react["React hook<br/>adapter<br/>[react.ts]"]
  node_react_shallow["React shallow<br/>hook helper<br/>[shallow.ts]"]
end

subgraph group_middleware["Middleware"]
  node_src_middleware_index["Middleware index<br/>middleware entrypoint<br/>[middleware.ts]"]
  node_mw_persist["Persist<br/>storage middleware<br/>[persist.ts]"]
  node_mw_devtools["Devtools<br/>integration middleware<br/>[devtools.ts]"]
  node_mw_immer["Immer<br/>update middleware<br/>[immer.ts]"]
  node_mw_redux["Redux<br/>reducer middleware<br/>[redux.ts]"]
  node_mw_combine["Combine<br/>composition middleware<br/>[combine.ts]"]
  node_mw_subscribe["Subscribe+selector<br/>subscription middleware"]
  node_mw_ssr["SSR safe<br/>runtime middleware<br/>[ssrSafe.ts]"]
end

subgraph group_surface["Public surface"]
  node_src_index["Main entry<br/>public entrypoint<br/>[index.ts]"]
  node_src_traditional["Traditional API<br/>compat entrypoint<br/>[traditional.ts]"]
  node_src_shallow["Shallow helper<br/>public helper<br/>[shallow.ts]"]
  node_src_types["Type defs<br/>types<br/>[types.d.ts]"]
end

subgraph group_ecosystem["Ecosystem"]
  node_docs["Docs site<br/>documentation<br/>[index.md]"]
  node_demo["Demo app<br/>example app<br/>[App.jsx]"]
  node_starter["Starter template<br/>example app<br/>[index.tsx]"]
  node_tests_react["React tests<br/>test suite<br/>[basic.test.tsx]"]
  node_tests_vanilla["Vanilla tests<br/>test suite<br/>[basic.test.ts]"]
  node_tooling["Release tooling<br/>build/test infra<br/>[rollup.config.mjs]"]
end

node_src_index -->|"exports core"| node_src_vanilla
node_src_index -->|"exports hooks"| node_src_react
node_src_index -->|"exports helper"| node_src_shallow
node_src_index -->|"types"| node_src_types
node_src_traditional -->|"legacy hooks"| node_src_react
node_src_shallow -->|"reuses"| node_vanilla_shallow
node_src_react -->|"wraps store"| node_src_vanilla
node_src_react -->|"uses"| node_react_shallow
node_src_middleware_index -->|"exports"| node_mw_persist
node_src_middleware_index -->|"exports"| node_mw_devtools
node_src_middleware_index -->|"exports"| node_mw_immer
node_src_middleware_index -->|"exports"| node_mw_redux
node_src_middleware_index -->|"exports"| node_mw_combine
node_src_middleware_index -->|"exports"| node_mw_subscribe
node_src_middleware_index -->|"exports"| node_mw_ssr
node_mw_persist -->|"wraps"| node_src_vanilla
node_mw_devtools -->|"wraps"| node_src_vanilla
node_mw_immer -->|"wraps"| node_src_vanilla
node_mw_redux -->|"wraps"| node_src_vanilla
node_mw_combine -->|"composes"| node_src_vanilla
node_mw_subscribe -->|"extends"| node_src_vanilla
node_mw_ssr -->|"adapts"| node_src_vanilla
node_docs -->|"documents"| node_src_index
node_docs -->|"documents"| node_src_middleware_index
node_docs -->|"documents"| node_src_react
node_demo -->|"showcases"| node_src_index
node_starter -->|"templates"| node_src_index
node_tests_react -->|"verifies"| node_src_react
node_tests_react -->|"verifies"| node_src_middleware_index
node_tests_vanilla -->|"verifies"| node_src_vanilla
node_tooling -->|"publishes"| node_docs
node_tooling -->|"runs"| node_tests_react
node_tooling -->|"runs"| node_tests_vanilla

click node_src_vanilla "https://github.com/pmndrs/zustand/blob/main/src/vanilla.ts"
click node_src_react "https://github.com/pmndrs/zustand/blob/main/src/react.ts"
click node_src_index "https://github.com/pmndrs/zustand/blob/main/src/index.ts"
click node_src_traditional "https://github.com/pmndrs/zustand/blob/main/src/traditional.ts"
click node_src_shallow "https://github.com/pmndrs/zustand/blob/main/src/shallow.ts"
click node_src_types "https://github.com/pmndrs/zustand/blob/main/src/types.d.ts"
click node_src_middleware_index "https://github.com/pmndrs/zustand/blob/main/src/middleware.ts"
click node_mw_persist "https://github.com/pmndrs/zustand/blob/main/src/middleware/persist.ts"
click node_mw_devtools "https://github.com/pmndrs/zustand/blob/main/src/middleware/devtools.ts"
click node_mw_immer "https://github.com/pmndrs/zustand/blob/main/src/middleware/immer.ts"
click node_mw_redux "https://github.com/pmndrs/zustand/blob/main/src/middleware/redux.ts"
click node_mw_combine "https://github.com/pmndrs/zustand/blob/main/src/middleware/combine.ts"
click node_mw_subscribe "https://github.com/pmndrs/zustand/blob/main/src/middleware/subscribeWithSelector.ts"
click node_mw_ssr "https://github.com/pmndrs/zustand/blob/main/src/middleware/ssrSafe.ts"
click node_react_shallow "https://github.com/pmndrs/zustand/blob/main/src/react/shallow.ts"
click node_vanilla_shallow "https://github.com/pmndrs/zustand/blob/main/src/vanilla/shallow.ts"
click node_docs "https://github.com/pmndrs/zustand/blob/main/docs/index.md"
click node_demo "https://github.com/pmndrs/zustand/blob/main/examples/demo/src/App.jsx"
click node_starter "https://github.com/pmndrs/zustand/blob/main/examples/starter/src/index.tsx"
click node_tests_react "https://github.com/pmndrs/zustand/blob/main/tests/basic.test.tsx"
click node_tests_vanilla "https://github.com/pmndrs/zustand/blob/main/tests/vanilla/basic.test.ts"
click node_tooling "https://github.com/pmndrs/zustand/blob/main/rollup.config.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_vanilla,node_vanilla_shallow toneBlue
class node_src_react,node_react_shallow toneAmber
class node_src_middleware_index,node_mw_persist,node_mw_devtools,node_mw_immer,node_mw_redux,node_mw_combine,node_mw_subscribe,node_mw_ssr toneMint
class node_src_index,node_src_traditional,node_src_shallow,node_src_types toneRose
class node_docs,node_demo,node_starter,node_tests_react,node_tests_vanilla,node_tooling toneIndigo
```

## Components

### Core runtime

Framework-agnostic store engine and public entrypoints.

- [Store engine](https://github.com/pmndrs/zustand/blob/main/src/vanilla.ts): store core · Framework-agnostic state creation, updates, subscriptions, and snapshots. · `src/vanilla.ts`
- [Vanilla shallow](https://github.com/pmndrs/zustand/blob/main/src/vanilla/shallow.ts): selector helper · Shallow equality helper used by non-React consumers. · `src/vanilla/shallow.ts`

### React layer

Hook adapters and selector helpers for React usage.

- [React hook](https://github.com/pmndrs/zustand/blob/main/src/react.ts): adapter · Binds the vanilla store to React components and slice-based re-renders. · `src/react.ts`
- [React shallow](https://github.com/pmndrs/zustand/blob/main/src/react/shallow.ts): hook helper · React-facing shallow comparison helper for preventing extra renders. · `src/react/shallow.ts`

### Middleware

Store enhancers that extend the core engine.

- [Middleware index](https://github.com/pmndrs/zustand/blob/main/src/middleware.ts): middleware entrypoint · Public middleware export surface and composition point. · `src/middleware.ts`
- [Persist](https://github.com/pmndrs/zustand/blob/main/src/middleware/persist.ts): storage middleware · Adds storage-backed state persistence and hydration. · `src/middleware/persist.ts`
- [Devtools](https://github.com/pmndrs/zustand/blob/main/src/middleware/devtools.ts): integration middleware · Connects store actions and state changes to Redux DevTools. · `src/middleware/devtools.ts`
- [Immer](https://github.com/pmndrs/zustand/blob/main/src/middleware/immer.ts): update middleware · Allows mutable-style updates through Immer semantics. · `src/middleware/immer.ts`
- [Redux](https://github.com/pmndrs/zustand/blob/main/src/middleware/redux.ts): reducer middleware · Provides reducer and dispatch-style store behavior. · `src/middleware/redux.ts`
- [Combine](https://github.com/pmndrs/zustand/blob/main/src/middleware/combine.ts): composition middleware · Composes partial store slices into one store. · `src/middleware/combine.ts`
- [Subscribe+selector](https://github.com/pmndrs/zustand/blob/main/src/middleware/subscribeWithSelector.ts): subscription middleware · Extends subscriptions with selector-aware notifications. · `src/middleware/subscribeWithSelector.ts`
- [SSR safe](https://github.com/pmndrs/zustand/blob/main/src/middleware/ssrSafe.ts): runtime middleware · Adjusts behavior for server-safe and mixed-rendering environments. · `src/middleware/ssrSafe.ts`

### Public surface

Package entrypoints, types, and compatibility shims.

- [Main entry](https://github.com/pmndrs/zustand/blob/main/src/index.ts): public entrypoint · Primary package export surface. · `src/index.ts`
- [Traditional API](https://github.com/pmndrs/zustand/blob/main/src/traditional.ts): compat entrypoint · Legacy/equality-function-focused public import path. · `src/traditional.ts`
- [Shallow helper](https://github.com/pmndrs/zustand/blob/main/src/shallow.ts): public helper · Shallow comparison helper exposed from the package root. · `src/shallow.ts`
- [Type defs](https://github.com/pmndrs/zustand/blob/main/src/types.d.ts): types · Package-level TypeScript declarations and API typing contract. · `src/types.d.ts`

### Ecosystem

Documentation, examples, tests, and release tooling.

- [Docs site](https://github.com/pmndrs/zustand/blob/main/docs/index.md): documentation · Guides, APIs, middleware references, and migration docs. · `docs/index.md`
- [Demo app](https://github.com/pmndrs/zustand/blob/main/examples/demo/src/App.jsx): example app · Interactive Vite showcase with code previews and UI examples. · `examples/demo/src/App.jsx`
- [Starter template](https://github.com/pmndrs/zustand/blob/main/examples/starter/src/index.tsx): example app · Minimal starter example for integrating the package. · `examples/starter/src/index.tsx`
- [React tests](https://github.com/pmndrs/zustand/blob/main/tests/basic.test.tsx): test suite · Behavioral tests for hook usage, selectors, middleware, and SSR. · `tests/basic.test.tsx`
- [Vanilla tests](https://github.com/pmndrs/zustand/blob/main/tests/vanilla/basic.test.ts): test suite · Core store and subscription tests for non-React usage. · `tests/vanilla/basic.test.ts`
- [Release tooling](https://github.com/pmndrs/zustand/blob/main/rollup.config.mjs): build/test infra · Bundling, test configuration, and CI workflows for publishing. · `rollup.config.mjs`

## Connections

- Main entry → Store engine: exports core · Main package surface re-exports the store engine.
- Main entry → React hook: exports hooks · Main package surface exposes React bindings.
- Main entry → Shallow helper: exports helper · Main package surface exposes shallow comparison helpers.
- Main entry → Type defs: types · Package API is typed from the root surface.
- Traditional API → React hook: legacy hooks · Traditional entrypoint builds on the React adapter layer.
- Shallow helper → Vanilla shallow: reuses · Root shallow helper delegates to the vanilla implementation.
- React hook → Store engine: wraps store · React adapter consumes the framework-agnostic store primitives.
- React hook → React shallow: uses · React hooks integrate shallow equality behavior.
- Middleware index → Persist: exports · Middleware entrypoint exposes persistence enhancer.
- Middleware index → Devtools: exports · Middleware entrypoint exposes devtools enhancer.
- Middleware index → Immer: exports · Middleware entrypoint exposes Immer enhancer.
- Middleware index → Redux: exports · Middleware entrypoint exposes Redux-style enhancer.
- Middleware index → Combine: exports · Middleware entrypoint exposes slice composition helper.
- Middleware index → Subscribe+selector: exports · Middleware entrypoint exposes selector subscription helper.
- Middleware index → SSR safe: exports · Middleware entrypoint exposes SSR-safe behavior.
- Persist → Store engine: wraps · Persistence is implemented as a store enhancer around the core engine.
- Devtools → Store engine: wraps · DevTools integration observes and augments the core store.
- Immer → Store engine: wraps · Immer middleware intercepts updates at the store boundary.
- Redux → Store engine: wraps · Redux-style dispatch is layered over the vanilla store.
- Combine → Store engine: composes · Combine builds larger stores from store slices.
- Subscribe+selector → Store engine: extends · Selector subscriptions extend the vanilla subscription model.
- SSR safe → Store engine: adapts · SSR-safe behavior adapts the core store for mixed renderers.
- Docs site → Main entry: documents · Docs describe the primary package API surface.
- Docs site → Middleware index: documents · Docs cover middleware integrations and behavior.
- Docs site → React hook: documents · Docs explain React hook usage and selectors.
- Demo app → Main entry: showcases · Demo app demonstrates real integration of the package API.
- Starter template → Main entry: templates · Starter example shows minimal package usage.
- React tests → React hook: verifies · React-focused tests validate hooks, selectors, middleware, and SSR behavior.
- React tests → Middleware index: verifies · Middleware behavior is exercised through React consumption paths.
- Vanilla tests → Store engine: verifies · Vanilla tests validate the low-level store contract.
- Release tooling → Docs site: publishes · Repo tooling supports docs generation and publishing workflows.
- Release tooling → React tests: runs · CI and test configuration execute the React test suites.
- Release tooling → Vanilla tests: runs · CI and test configuration execute the vanilla test suites.
