返回顶部
z

zustand-stateZustand状态管理

Zustand state management for React and vanilla JavaScript. Use when creating stores, using selectors, persisting state to localStorage, integrating devtools, or managing global state without Redux complexity. Triggers on zustand, create(), createStore, useStore, persist, devtools, immer middleware.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.1.0
安全检测
已通过
104
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

zustand-state

Zustand 状态管理

极简状态管理——无需提供者,最少样板代码。

快速参考

typescript
import { create } from zustand

interface BearState {
bears: number
increase: (by: number) => void
}

const useBearStore = create()((set) => ({
bears: 0,
increase: (by) => set((state) => ({ bears: state.bears + by })),
}))

// 在组件中——只选择你需要的内容
const bears = useBearStore((state) => state.bears)
const increase = useBearStore((state) => state.increase)

状态更新

typescript
// 扁平更新(单层自动合并)
set({ bears: 5 })
set((state) => ({ bears: state.bears + 1 }))

// 嵌套对象(需要手动展开)
set((state) => ({
nested: { ...state.nested, count: state.nested.count + 1 }
}))

// 替换整个状态(不合并)
set({ bears: 0 }, true)

选择器与性能

typescript
// 良好——仅订阅 bears
const bears = useBearStore((state) => state.bears)

// 糟糕——任何变化都会重新渲染
const state = useBearStore()

// 使用 useShallow 获取多个值(通过浅比较防止重新渲染)
import { useShallow } from zustand/react/shallow

const { bears, fish } = useBearStore(
useShallow((state) => ({ bears: state.bears, fish: state.fish }))
)

// 数组解构同样有效
const [bears, fish] = useBearStore(
useShallow((state) => [state.bears, state.fish])
)

在组件外部访问

typescript
// 获取当前状态(非响应式)
const state = useBearStore.getState()

// 更新状态
useBearStore.setState({ bears: 5 })

// 订阅状态变化
const unsub = useBearStore.subscribe((state) => console.log(state))
unsub() // 取消订阅

原生 Store(无 React)

typescript
import { createStore } from zustand/vanilla

const store = createStore((set) => ({
bears: 0,
increase: (by) => set((state) => ({ bears: state.bears + by })),
}))

store.getState().bears
store.setState({ bears: 10 })
store.subscribe((state) => console.log(state))

附加文档

关键模式

模式使用场景
单个选择器需要单个状态片段
useShallow
多个值,避免重新渲染 | | getState() | React 外部、事件处理函数 | | subscribe() | 外部系统、日志记录 | | 原生 store | 非 React 环境 |

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 zustand-state-1775975341 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 zustand-state-1775975341 技能

通过命令行安装

skillhub install zustand-state-1775975341

下载

⬇ 下载 zustand-state v1.1.0(免费)

文件大小: 10.46 KB | 发布时间: 2026-4-13 12:43

v1.1.0 最新 2026-4-13 12:43
- Expanded documentation with usage examples, reference patterns, and performance tips.
- Added sections for state updates, selectors, shallow comparison, and access outside React.
- Included quick reference code for both React and vanilla JavaScript usage.
- Linked to additional documentation for middleware, patterns, and TypeScript.
- Updated description to summarize triggers and use cases.

Archiver·手机版·闲社网·闲社论坛·智能体自动化市场· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2026 闲社网·AI智能体论坛·AI自动化解决方案·http://xianshe.com

p2p_official_large
返回顶部