zustand

Standardize Zustand store slices with action layering and optimistic updates.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/kingheu0818-sketch/lobehub_yu --skill zustand-kingheu0818-sketch
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: zustand
Source: https://github.com/kingheu0818-sketch/lobehub_yu/tree/main/.agents/skills/zustand
Command: npx skills add https://github.com/kingheu0818-sketch/lobehub_yu --skill zustand-kingheu0818-sketch

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the problem of inconsistent or hard-to-debug Zustand store behavior by standardizing how actions, dispatch reducers, optimistic updates, and slice composition should be implemented in LobeHub.

Core Features & Use Cases

  • Action hierarchy: Separate UI-facing public actions, internal business actions (prefixed with internal_), and state update dispatchers (internal_dispatch*), so intent is clear and responsibilities stay isolated.
  • Optimistic update pattern: Apply immediate UI updates for create/update flows (including temp IDs) while reconciling with backend services for consistency.
  • Reducer vs set guidance: Use reducer-style updates for maps/lists such as messagesMap/topicMaps and use direct set updates for simple primitives to reduce complexity and stale state issues.
  • Class-based slice actions: Migrate slices from plain StateCreator objects to class-based action implementations with private fields and typed public method exposure via Pick, then compose them correctly using flattenActions.
  • Composition & selectors: Integrate multiple action classes into one store slice using flattenActions, and structure state access through maps, selectors, and well-defined store augmentation types.

Use Case Example: Build a chat topic flow where creating a topic updates topicMaps optimistically, calls the backend, and refreshes messages/topics to prevent stale reads across useChatStore/useUserStore/useGlobalStore.

Quick Start

Use the zustand Skill conventions to implement a new slice under src/store/** by defining a public/internal/dispatch action flow, applying optimistic updates where safe, and composing class-based slices via flattenActions.

Frequently Asked Questions about zustand

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure Zustand store actions to separate UI updates from state dispatching?

Structure Zustand store actions by separating UI-facing public actions, internal business actions prefixed with internal_, and state update dispatchers prefixed with internal_dispatch. This action layering keeps intent clear and responsibilities isolated for predictable state management.

How do I implement optimistic updates for chat stores using Zustand?

Implement optimistic updates in Zustand by applying immediate UI changes with temporary IDs for create or update flows, then reconciling with backend services. This updates maps like messagesMap or topicMaps instantly while ensuring backend consistency.

When should I use a reducer pattern versus direct set updates in Zustand state management?

Use reducer-style updates in Zustand for complex state structures like maps and lists such as messagesMap to prevent stale state issues. Use direct set updates for simple primitive values to reduce overall code complexity.

What is the best way to compose multiple Zustand slices with class-based actions?

Compose multiple Zustand slices by migrating from plain StateCreator objects to class-based action implementations, then integrate them into one store slice using flattenActions. Expose typed public methods via Pick for correct composition.

How does cross-slice orchestration work for chat, topic, and message updates in Zustand?

Cross-slice orchestration in Zustand coordinates state updates across stores like useChatStore and useUserStore. When a backend call completes, it refreshes messages and topics to prevent stale reads and maintain global consistency.

Can I use TypeScript to enforce typed public method exposure in Zustand class-based slices?

Yes, TypeScript enforces typed public method exposure in Zustand class-based slices by using Pick. This restricts external access to private fields while allowing typed integration when composing slices via flattenActions.