📦 Zustand State — 状态管理助手

v1.1.0

为 React 与原生 JavaScript 提供 Zustand 状态管理的速查文档与代码示例,涵盖 store 创建、selector 使用、localStorage 持久化、devtools 集成等常见场景,帮助开发者快速上手并规范实践。

0· 96·1 当前·1 累计
anderskev 头像by @anderskev (Kevin Anderson)
下载技能包
最后更新
2026/3/28
0
安全扫描
VirusTotal
无害
查看报告
OpenClaw
安全
high confidence
这是一份仅含文档与使用指引的技能,提供 Zustand 状态库的使用模式与示例;其所需范围与说明均符合该目的,未索要额外凭据或安装。
评估建议
该技能为 Zustand 的文档与示例代码集合,内容与其描述一致,未请求密钥或执行安装。安装前请注意:1)缺失来源/主页,若偏好官方或可追溯来源,请直接使用上游 Zustand 文档或包仓库;2)示例中的持久化代码(localStorage/asyncStorage)会在浏览器存数据,复制到应用前请审查所存内容,避免敏感信息;3)技能本身不执行代码,仅提供示例,但智能体可能据此生成代码,运行前请自行检查。若需来源保障,建议选择指向官方仓库或维护者的技能。...
详细分析 ▾
用途与能力
名称与描述与所提供材料一致:文件为 React 与原生 JS 中使用 Zustand 的文档与代码示例。无需环境变量、二进制或安装步骤,符合文档/教程类技能特征。注意:技能缺失来源/主页,可追溯性降低,但不违背其声明目的。
指令范围
SKILL.md 与参考文件包含创建 store、中间件、持久化(localStorage)、开发工具、测试及 TypeScript 模式的代码片段与指导。指令未让智能体读取无关系统文件或外传数据,也未要求向第三方端点发起网络请求(示例中的 fetch('/api/data') 仅为典型用法演示)。未出现“收集所需任何上下文”等宽泛授权措辞。
安装机制
未提供安装规范;此为纯文档说明。不会通过安装器下载或写入磁盘,安装风险极低。
凭证需求
技能未声明所需环境变量、凭据或配置路径。示例提及 localStorage 与示意性的 asyncStorage 抽象(用于持久化演示),但无需访问密钥或无关服务。
持久化与权限
always 为 false,技能未请求持久化系统驻留或修改其他技能配置。它是按需调用的文档类技能,具备常规自治设置。
安全有层次,运行前请审查代码。

运行时依赖

无特殊依赖

版本

latestv1.1.02026/3/28

- 扩展文档,新增使用示例、参考模式与性能提示。 - 增加状态更新、selector、浅比较及 React 外访问章节。 - 提供 React 与原生 JavaScript 的速查代码。 - 链接至中间件、模式与 TypeScript 的额外文档。 - 更新描述以概括触发条件与使用场景。

无害

安装命令

点击复制
官方npx clawhub@latest install zustand-state
镜像加速npx clawhub@latest install zustand-state --registry https://cn.longxiaskill.com

技能文档

# Zustand 状态管理 极简状态管理——无需 Provider,样板代码最少。 ## 快速参考 ``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 })), })) // In component - select only what you need const bears = useBearStore((state) => state.bears) const increase = useBearStore((state) => state.increase) ` ## 状态更新 `typescript // Flat updates (auto-merged at one level) set({ bears: 5 }) set((state) => ({ bears: state.bears + 1 })) // Nested objects (manual spread required) set((state) => ({ nested: { ...state.nested, count: state.nested.count + 1 } })) // Replace entire state (no merge) set({ bears: 0 }, true) ` ## 选择器与性能 `typescript // Good - subscribes only to bears const bears = useBearStore((state) => state.bears) // Bad - rerenders on any change const state = useBearStore() // Multiple values with useShallow (prevents rerenders with shallow comparison) import { useShallow } from 'zustand/react/shallow' const { bears, fish } = useBearStore( useShallow((state) => ({ bears: state.bears, fish: state.fish })) ) // Array destructuring also works const [bears, fish] = useBearStore( useShallow((state) => [state.bears, state.fish]) ) ` ## 在组件外访问 `typescript // Get current state (non-reactive) const state = useBearStore.getState() // Update state useBearStore.setState({ bears: 5 }) // Subscribe to changes const unsub = useBearStore.subscribe((state) => console.log(state)) unsub() // unsubscribe ` ## Vanilla 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)) ` ## 额外文档 - Middleware:查看 references/middleware.md 了解 persist、devtools、immer - Patterns:查看 references/patterns.md 了解 slices、测试、最佳实践 - TypeScript:查看 references/typescript.md 了解高级类型模式 ## 关键模式 | 模式 | 使用场景 | |---------|-------------| | Single selector | 只需一段状态 | | useShallow | 多个值,避免重渲染 | | getState() | React 外部、事件处理器 | | subscribe()`` | 外部系统、日志 | | Vanilla store | 非 React 环境 |

数据来源ClawHub ↗ · 中文优化:龙虾技能库