zustand-state by existential-birds/beagle
npx skills add https://github.com/existential-birds/beagle --skill zustand-state极简状态管理 - 无需提供者,样板代码最少。
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 })),
}))
// 在组件中 - 仅选择所需内容
const bears = useBearStore((state) => state.bears)
const increase = useBearStore((state) => state.increase)
// 扁平更新(自动合并一层)
set({ bears: 5 })
set((state) => ({ bears: state.bears + 1 }))
// 嵌套对象(需要手动展开)
set((state) => ({
nested: { ...state.nested, count: state.nested.count + 1 }
}))
// 替换整个状态(不合并)
set({ bears: 0 }, true)
// 良好 - 仅订阅 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])
)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// 获取当前状态(非响应式)
const state = useBearStore.getState()
// 更新状态
useBearStore.setState({ bears: 5 })
// 订阅变更
const unsub = useBearStore.subscribe((state) => console.log(state))
unsub() // 取消订阅
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() | 外部系统、日志记录 |
| 纯 JavaScript 存储 | 非 React 环境 |
每周安装量
111
代码仓库
GitHub 星标数
41
首次出现
2026年1月20日
安全审计
安装于
gemini-cli94
opencode92
codex90
github-copilot86
cursor83
claude-code81
Minimal state management - no providers, minimal boilerplate.
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)
// 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)
// 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])
)
// 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
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))
| 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 |
Weekly Installs
111
Repository
GitHub Stars
41
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
gemini-cli94
opencode92
codex90
github-copilot86
cursor83
claude-code81
TanStack Query v5 完全指南:React 数据管理、乐观更新、离线支持
2,500 周安装