How pmndrs/zustand works
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: middleware.ts Middleware index · vanilla.ts Store engine · index.ts Main entry · react.ts React hook
Components 22
Core runtime
Framework-agnostic store engine and public entrypoints.
- Store engine · Framework-agnostic state creation, updates, subscriptions, and snapshots.
src/vanilla.ts - Vanilla shallow · Shallow equality helper used by non-React consumers.
src/vanilla/shallow.ts
React layer
Hook adapters and selector helpers for React usage.
- React hook · Binds the vanilla store to React components and slice-based re-renders.
src/react.ts - React shallow · React-facing shallow comparison helper for preventing extra renders.
src/react/shallow.ts
Middleware
Store enhancers that extend the core engine.
- Middleware index · Public middleware export surface and composition point.
src/middleware.ts - Persist · Adds storage-backed state persistence and hydration.
src/middleware/persist.ts - Devtools · Connects store actions and state changes to Redux DevTools.
src/middleware/devtools.ts - Immer · Allows mutable-style updates through Immer semantics.
src/middleware/immer.ts - Redux · Provides reducer and dispatch-style store behavior.
src/middleware/redux.ts - Combine · Composes partial store slices into one store.
src/middleware/combine.ts - Subscribe+selector · Extends subscriptions with selector-aware notifications.
src/middleware/subscribeWithSelector.ts - SSR safe · Adjusts behavior for server-safe and mixed-rendering environments.
src/middleware/ssrSafe.ts
Public surface
Package entrypoints, types, and compatibility shims.
- Main entry · Primary package export surface.
src/index.ts - Traditional API · Legacy/equality-function-focused public import path.
src/traditional.ts - Shallow helper · Shallow comparison helper exposed from the package root.
src/shallow.ts - Type defs · Package-level TypeScript declarations and API typing contract.
src/types.d.ts
Ecosystem
Documentation, examples, tests, and release tooling.
- Docs site · Guides, APIs, middleware references, and migration docs.
docs/index.md - Demo app · Interactive Vite showcase with code previews and UI examples.
examples/demo/src/App.jsx - Starter template · Minimal starter example for integrating the package.
examples/starter/src/index.tsx - React tests · Behavioral tests for hook usage, selectors, middleware, and SSR.
tests/basic.test.tsx - Vanilla tests · Core store and subscription tests for non-React usage.
tests/vanilla/basic.test.ts - Release tooling · Bundling, test configuration, and CI workflows for publishing.
rollup.config.mjs
Connections 33
- 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.