zustand-store-scaffold
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZustand Store Scaffold
Zustand Store 脚手架
Generate type-safe class-based Zustand stores with for prototype-safe slice composition.
flattenActions使用生成类型安全的基于类的Zustand store,实现原型安全的切片组合。
flattenActionsQuick Start
快速开始
Web Pattern (Component-level Store)
Web模式(组件级Store)
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern web \
--name ToolList \
--path web/src/pages/_components/ToolList/storeGenerates:
- —
types.tstype definitionStoreSetter<T> - — Prototype-safe action flattener
utils/flattenActions.ts - — Class-based store with
index.ts+ActionImplflattenActions - — React Context and typed
context.tshookuseContext - — Memo-wrapped Provider component
provider.tsx
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern web \
--name ToolList \
--path web/src/pages/_components/ToolList/store生成文件:
- —
types.ts类型定义StoreSetter<T> - — 原型安全的操作扁平化工具
utils/flattenActions.ts - — 结合
index.ts与ActionImpl的基于类的storeflattenActions - — React Context及类型化
context.ts钩子useContext - — 经过Memo包装的Provider组件
provider.tsx
Core Pattern (Slice-based Store)
Core模式(切片式Store)
Single Slice:
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name CoreAgent \
--path packages/ag-ui-view/src/core/helpers/isomorphic/storeMultiple Slices (Interactive):
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name AppStore \
--path src/store单切片:
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name CoreAgent \
--path packages/ag-ui-view/src/core/helpers/isomorphic/store多切片(交互式):
bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name AppStore \
--path src/storeWill prompt: Enter slice names (comma-separated, or press Enter for 'core'):
会提示:输入切片名称(逗号分隔,或按Enter使用默认值'core'):
Example input: auth,user,ui
示例输入: auth,user,ui
**Multiple Slices (Direct):**
```bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name AppStore \
--path src/store \
--slices auth,user,uiGenerates:
- —
types.tstype definitionStoreSetter<T> - — Prototype-safe action flattener
utils/flattenActions.ts - — Store factory with
index.tsslice compositionflattenActions - — Class-based slice with
slices/[name].ts+ActionImplfields#private
**多切片(直接指定):**
```bash
python3 ~/.claude/skills/zustand-store-scaffold/scripts/scaffold_zustand_store.py \
--pattern core \
--name AppStore \
--path src/store \
--slices auth,user,ui生成文件:
- —
types.ts类型定义StoreSetter<T> - — 原型安全的操作扁平化工具
utils/flattenActions.ts - — 结合
index.ts切片组合的Store工厂flattenActions - — 包含
slices/[name].ts与ActionImpl字段的基于类的切片#private
Options
选项说明
| Option | Required | Description | Example |
|---|---|---|---|
| Yes | Store pattern ( | |
| Yes | Store name in PascalCase | |
| Yes | Target directory path | |
| No | Comma-separated slice names (core only) | |
| No | Overwrite existing files | |
Note: For pattern without , the script interactively asks for slice names. Shared files (, ) are skipped if they already exist (unless ).
core--slicestypes.tsutils/flattenActions.ts--force| 选项 | 是否必填 | 描述 | 示例 |
|---|---|---|---|
| 是 | Store模式( | |
| 是 | 采用大驼峰命名的Store名称 | |
| 是 | 目标目录路径 | |
| 否 | 逗号分隔的切片名称(仅core模式可用) | |
| 否 | 覆盖已存在的文件 | |
注意: 对于未指定的core模式,脚本会交互式询问切片名称。共享文件(、)若已存在则会跳过(除非使用)。
--slicestypes.tsutils/flattenActions.ts--forceChoose the Right Pattern
选择合适的模式
| Pattern | Use When | Location Pattern | Slices |
|---|---|---|---|
| web | Component-level state with Provider | | N/A |
| core | Isomorphic/shared state with slices | | Single or multiple |
| 模式 | 使用场景 | 路径模式 | 切片支持 |
|---|---|---|---|
| web | 需要Provider的组件级状态 | | 不支持 |
| core | 同构/共享状态,支持切片 | | 单切片或多切片 |
When to Use Multiple Slices (Core Pattern)
何时使用多切片(Core模式)
Use multiple slices to organize complex state by feature domains:
| Scenario | Recommended Slices |
|---|---|
| Authentication app | |
| E-commerce store | |
| Dashboard | |
| Chat application | |
使用多切片可按功能领域组织复杂状态:
| 场景 | 推荐切片 |
|---|---|
| 认证应用 | |
| 电商商城 | |
| 数据看板 | |
| 聊天应用 | |
Generated Files
生成文件说明
| File | Purpose |
|---|---|
| |
| |
| Store factory combining initial state + |
| |
| React Context + typed selector hook |
| Memo-wrapped Provider with ref-stable store creation |
| 文件 | 用途 |
|---|---|
| |
| |
| 结合初始状态与 |
| |
| React Context + 类型化选择器钩子 |
| 经过Memo包装的Provider,创建引用稳定的store |
Post-Generation Steps
生成后步骤
-
Define state ininterface (core) or
*SliceStateinterface (web):*StoreStatetsexport interface CoreSliceState { agents: Agent[] selectedId: string | null } -
Add action methods as arrow functions inclass:
*ActionImpltsexport class CoreActionImpl { readonly #set: StoreSetter<CoreSlice> readonly #get: () => CoreSlice // ...constructor selectAgent = (id: string): void => { this.#set({ selectedId: id }) } getSelectedAgent = (): Agent | undefined => { const { agents, selectedId } = this.#get() return agents.find((a) => a.id === selectedId) } } -
Set initial state in the store factory or config:ts
const store = createCoreAgentStore({ initialState: { agents: [], selectedId: null } })
-
定义状态 在接口(core模式)或
*SliceState接口(web模式)中:*StoreStatetsexport interface CoreSliceState { agents: Agent[] selectedId: string | null } -
添加操作方法 在类中添加箭头函数:
*ActionImpltsexport class CoreActionImpl { readonly #set: StoreSetter<CoreSlice> readonly #get: () => CoreSlice // ...构造函数 selectAgent = (id: string): void => { this.#set({ selectedId: id }) } getSelectedAgent = (): Agent | undefined => { const { agents, selectedId } = this.#get() return agents.find((a) => a.id === selectedId) } } -
设置初始状态 在Store工厂或配置中:ts
const store = createCoreAgentStore({ initialState: { agents: [], selectedId: null } })
Usage Examples
使用示例
Web Store Usage
Web Store 使用方式
tsx
import Provider from './store/provider'
import { useToolListContext } from './store/context'
function ToolListPage() {
return (
<Provider>
<ToolListContent />
</Provider>
)
}
function ToolListContent() {
const toolList = useToolListContext((s) => s.toolList)
const setToolList = useToolListContext((s) => s.setToolList)
// Go to Definition on setToolList → lands directly on ActionImpl method
}tsx
import Provider from './store/provider'
import { useToolListContext } from './store/context'
function ToolListPage() {
return (
<Provider>
<ToolListContent />
</Provider>
)
}
function ToolListContent() {
const toolList = useToolListContext((s) => s.toolList)
const setToolList = useToolListContext((s) => s.setToolList)
// 对setToolList执行跳转定义 → 直接定位到ActionImpl方法
}Core Store Usage
Core Store 使用方式
ts
import { createCoreAgentStore } from './store'
const store = createCoreAgentStore({
initialState: { agents: [], selectedId: null }
})
// Vanilla JS
const state = store.getState()
state.selectAgent('agent-1')
// React (with useStore)
import { useStore } from 'zustand'
function AgentList() {
const agents = useStore(store, (s) => s.agents)
const selectAgent = useStore(store, (s) => s.selectAgent)
}ts
import { createCoreAgentStore } from './store'
const store = createCoreAgentStore({
initialState: { agents: [], selectedId: null }
})
// 原生JS使用
const state = store.getState()
state.selectAgent('agent-1')
// React中使用(结合useStore)
import { useStore } from 'zustand'
function AgentList() {
const agents = useStore(store, (s) => s.agents)
const selectAgent = useStore(store, (s) => s.selectAgent)
}References
参考资料
- See for detailed pattern documentation
references/class-based-pattern.md - See for complete code examples
references/store-patterns.md - Reference PR: lobehub#12081
- 查看获取详细的模式文档
references/class-based-pattern.md - 查看获取完整的代码示例
references/store-patterns.md - 参考PR: lobehub#12081