Back to Skills

zustand

LobeHub Zustand store conventions. Use when editing src/store, store slices, public/internal actions, dispatch actions, flattenActions, optimistic updates, selectors, maps, or class action migration.

81,331stars15,776forksUpdated 8/6/2026

Security Assessment

Safe(97/100)
Security Score97/100

About zustand

This skill documents LobeHub's Zustand state-management conventions for the lobe-chat codebase, giving contributors and AI agents a consistent pattern for writing and editing store slices. It defines a three-tier action hierarchy: public actions (verb-named interfaces called by UI components, responsible for validation and orchestration), internal actions (`internal_` prefixed core business logic handling optimistic updates, service calls, and error handling, never called directly by UI), and dispatch methods (`internal_dispatch*` handlers that call reducers and update the store). The problem it solves is keeping a large, evolving store organized and predictable as the codebase grows.

The guide covers when to use the reducer pattern versus a simple `set` (reducers for object lists/maps, optimistic updates, and complex transitions; simple set for booleans and single values), the standard optimistic-update flow of updating the frontend immediately, calling the backend, then refreshing for consistency — with the explicit note that destructive delete operations should not use optimistic updates. It documents naming conventions for actions and state (ID arrays, maps, active-item fields, init flags), and an ongoing migration from plain StateCreator objects to class-based actions that receive `(set, get, api)` in a constructor, use private fields, and are composed via a `flattenActions` helper. Companion reference files detail action patterns (including SWR integration, cache invalidation via mutate, loading-state management, and the reducer pattern with immer's produce) and slice organization (top-level store aggregation, single-slice directory structure, map structures, and best practices).

The target users are front-end engineers and coding agents editing `src/store` in lobe-chat. It is a benign code-convention and architecture reference with illustrative TypeScript snippets and no risky operations.

FAQ

What are the three tiers of actions?

Public actions (verb-named UI interfaces handling validation and orchestration), internal actions (`internal_` prefixed core business logic, not called directly by UI), and dispatch methods (`internal_dispatch*` that call reducers and update the store).

When should I use a reducer versus a simple set?

Use the reducer pattern for managing object lists/maps, optimistic updates, and complex state transitions; use a simple `set` for toggling booleans, updating simple values, and setting single state fields.

How are optimistic updates handled?

Update the frontend immediately, then call the backend service, then refresh for consistency. Delete operations are explicitly excluded from optimistic updates because they are destructive and complex to recover from.

What is the class-based action migration?

The project is migrating slices from plain StateCreator objects to class-based actions that receive (set, get, api) in the constructor, use private fields to avoid leaking internals, and are composed with the `flattenActions` helper rather than spreading class instances.

Where are the detailed patterns documented?

In two reference files: `references/action-patterns.md` (SWR integration, cache invalidation, loading state, reducer pattern) and `references/slice-organization.md` (store aggregation, single-slice structure, and best practices).

All Files

3 files
references/action-patterns.md2.9 KB
View
references/slice-organization.md3.2 KB
View
SKILL.md5.6 KB
View

Install zustand

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill