返回顶部
z

zustand-state

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
安全检测
已通过
80
下载量
0
收藏
概述
安装方式
版本历史

zustand-state

# Zustand State Management Minimal state management - no providers, minimal boilerplate. ## Quick Reference ```typescript import { create } from 'zustand' interface BearState { bears: number increase: (by: number) => void } const useBearStore = create<BearState>()((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) ``` ## State Updates ```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) ``` ## Selectors & Performance ```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]) ) ``` ## Access Outside Components ```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 (No 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)) ``` ## Additional Documentation - **Middleware**: See [references/middleware.md](references/middleware.md) for persist, devtools, immer - **Patterns**: See [references/patterns.md](references/patterns.md) for slices, testing, best practices - **TypeScript**: See [references/typescript.md](references/typescript.md) for advanced typing patterns ## Key Patterns | Pattern | When to Use | |---------|-------------| | Single selector | One piece of state needed | | `useShallow` | Multiple values, avoid rerenders | | `getState()` | Outside React, event handlers | | `subscribe()` | External systems, logging | | Vanilla store | Non-React environments |

标签

skill ai

通过对话安装

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

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

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

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

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

通过命令行安装

skillhub install zustand-state-1775975341

下载 Zip 包

⬇ 下载 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-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部