agentleFS
ControllableModelControllableModel/SWE-Skills-Bench/skills/zustand-state-management/SKILL.md

zustand-state-management

Skill8 starsChanged 46 days agoMITView on GitHub

Best practices for Zustand state management in React and Next.js applications with TypeScript.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

12 rules. 0 are common, 11 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 11

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use Zustand for lightweight, flexible state management

    line 12
  2. Very specific:

    Use descriptive variable names with auxiliary verbs like isLoading, hasError

    line 15
  3. Distinctive:

    Keep stores focused and modular

    line 41
  4. Distinctive:

    Use selectors to prevent unnecessary re-renders

    line 42
  5. Distinctive:

    Use shallow equality for selecting multiple values

    line 48
  6. Distinctive:

    Use zustand/middleware for persistence and devtools

    line 51
  7. Distinctive:

    Use shallow comparison for object selections

    line 56
  8. Distinctive:

    Avoid selecting the entire store

    line 57
  9. Distinctive:

    Use try-catch in async actions

    line 91
  10. Distinctive:

    Test stores independently of components

    line 96
  11. Distinctive:

    Verify state transitions and actions

    line 98
  12. Distinctive:

    Test middleware behavior separately

    line 99

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Zustand State Management

You are an expert in Zustand state management for React and Next.js applications.

Core Principles

  • Use Zustand for lightweight, flexible state management
  • Minimize useEffect and setState; prioritize derived state and memoization
  • Implement functional, declarative patterns avoiding classes
  • Use descriptive variable names with auxiliary verbs like isLoading, hasError

Store Design

Basic Store Structure

import { create } from 'zustand'

interface BearState {
  bears: number
  isLoading: boolean
  hasError: boolean
  increase: () => void
  reset: () => void
}

const useBearStore = create<BearState>((set) => ({
  bears: 0,
  isLoading: false,
  hasError: false,
  increase: () => set((state) => ({ bears: state.bears + 1 })),
  reset: () => set({ bears: 0 }),
}))

Best Practices

  • Keep stores focused and modular
  • Use selectors to prevent unnecessary re-renders
  • Implement middleware for persistence, logging, or devtools
  • Separate actions from state when stores grow complex

Integration with React

  • Use shallow equality for selecting multiple values
  • Combine with TanStack React Query for server state
  • Implement proper TypeScript interfaces for type safety
  • Use zustand/middleware for persistence and devtools

Performance Optimization

  • Select only the state you need in components
  • Use shallow comparison for object selections
  • Avoid selecting the entire store
  • Memoize computed values when necessary

Middleware Usage

Persistence

import { persist } from 'zustand/middleware'

const useStore = create(
  persist(
    (set) => ({
      // state and actions
    }),
    { name: 'store-key' }
  )
)

DevTools

import { devtools } from 'zustand/middleware'

const useStore = create(
  devtools((set) => ({
    // state and actions
  }))
)

Error Handling

  • Handle errors at function start using early returns and guard clauses
  • Implement error states within stores
  • Use try-catch in async actions
  • Provide meaningful error messages

Testing

  • Test stores independently of components
  • Mock Zustand stores in component tests
  • Verify state transitions and actions
  • Test middleware behavior separately

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.