zustand

A set of coding rules for Zustand, a small JavaScript and TypeScript library for storing shared application state. It covers store structure, selectors, actions, middleware, and persistence.

In plain words
What is it for?
Use it when creating or reviewing Zustand stores, state selectors, asynchronous actions, derived values, middleware, and persisted state.
Why use it?
It helps keep shared state focused and prevents components from re-rendering whenever unrelated state changes.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/zustand
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 656 This file is loaded in full into every session.
When invoked 656 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00656 $0.00656
Opus 5 $0.00328 $0.00328
Sonnet 5 $0.00131 $0.00131
Haiku 4.5 $0.00066 $0.00066

Measured 2d ago against content hash cc6b2b311f08, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

zustand scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules-mdc/frameworks/zustand.mdc · 58 lines

How it starts

The opening of the file, as written. The whole thing — 58 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Zustand Cursor Rules

You are an expert in Zustand state management. Follow these rules:

Store Design

  • One store per domain: useAuthStore, useCartStore, useUIStore
  • Keep stores small and focused — split rather than grow
  • Define the store interface explicitly: interface AuthState { user: User | null; login: (creds: Creds) => Promise }
  • Colocate actions with state in the same store — no separate action files
  • Export the hook directly: export const useAuthStore = create()(...)

Selectors

  • Always use selectors to pick specific state: useAuthStore(state => state.user)
  • Never use useAuthStore() without a selector — it re-renders on every state change
  • Create reusable selectors: const selectUser = (state: AuthState) => state.user
  • For multiple values, use useShallow: useAuthStore(useShallow(state => ({ user: state.user, loading: state.loading })))
  • Derive computed values in selectors, not in the store

Actions

  • Define actions inside create() — they have direct access to set and get
  • Use set(state => ({ count: state.count + 1 })) for updates based on current state
  • Use get() inside async actions to read current state mid-operation
  • Never call set() in a loop — batch updates into a single set() call
  • Actions should be the only way to modify state — no external set() calls

Middleware

  • Stack middleware with the correct order: persist(immer(devtools(...)))
  • Use immer middleware for deeply nested state updates
  • Enable devtools in development only: devtools(..., { enabled: process.env.NODE_ENV === 'development' })
  • Use subscribeWithSelector for reacting to specific state changes outside React

Persist

  • Use persist() for state that survives page refresh (auth tokens, preferences, cart)
  • Always set a unique name for each persisted store
  • Use partialize to persist only what's needed: partialize: (state) => ({ token: state.token })
  • Set a version number and define migrate() for schema changes
  • Use skipHydration: true and manually call rehydrate() if SSR hydration conflicts occur

Read the full file on GitHub · 58 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 58 lines · 656 tokens per session scan A cc6b2b311f08

Subscribe to this mod's changes

zustand is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 656 tokens to every session, about $0.0033 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.