zustand

Implements LobeHub Zustand store conventions for slices, actions, and optimistic updates.

74|11|Updated Jul 4, 2024
One-click install
npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill zustand-opensourceagi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: zustand
Source: https://github.com/OpenSourceAGI/qwksearch-research-agent/tree/main/apps/qwk-in-lobe/.agents/skills/zustand
Command: npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill zustand-opensourceagi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Maintaining consistent state management patterns across a large Zustand codebase is difficult, especially when coordinating public actions, internal logic, dispatch reducers, and optimistic updates across many store slices. ## Core Features & Use Cases - Action Hierarchy Conventions: Enforces a three-layer action model with public actions, internal_* business logic, and internal_dispatch* state updaters. - Optimistic Update Patterns: Provides standard flows for create, update, and delete operations with temporary IDs, service calls, and refresh-based consistency. - Class-Based Slice Migration: Guides migration from plain StateCreator objects to class-based actions composed with flattenActions. - Use Case: When adding a new chat slice to src/store, follow the slice organization references to structure initialState, selectors, reducers, and actions consistently with the rest of the codebase. ## Quick Start Use the zustand skill to create a new store slice with optimistic updates following the LobeHub action hierarchy conventions.

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 slices in a large TypeScript app?

Organize each slice with initialState.ts, action.ts, selectors.ts, and an optional reducer.ts under src/store slices directories. Aggregate slice states and actions in top-level initialState.ts and store.ts files, and export selectors through an xxxSelectors object.

How to implement optimistic updates in Zustand?

Dispatch a temporary state update immediately with a generated temp ID, then call the backend service, and finally refresh the data for consistency. Delete operations should skip optimistic updates because recovery from destructive actions is complex.

When should I use a reducer versus a simple set in Zustand?

Use the reducer pattern when managing object lists or maps, performing optimistic updates, or handling complex state transitions. Use a simple set call for toggling booleans, updating single values, or setting individual state fields.

How do I migrate Zustand slices to class-based actions?

Define a class that receives set, get, and api in its constructor, store them as private fields, and expose only public methods via Pick typing. Compose multiple class instances in the store using flattenActions instead of spreading them.

Why should UI components not call internal_ actions directly?

Internal actions contain core business logic like optimistic updates and service calls that require orchestration. Public actions handle parameter validation and flow control, so bypassing them risks inconsistent state and unvalidated inputs.