mcpField
Z
zustand-state

zustand-state

github.com/existential-birds開発85255 回インストール11 週間前に更新
リポジトリ

zustand-state について

zustand-stateは、TypeScriptとReactでのZustandによる状態管理を解説するスキルです。ストア更新、セレクター、コンポーネント外からの操作、バニラストアに加え、persistとdevtoolsの確認基準を扱います。

主な機能

  1. 01Zustandストアの作成と更新
  2. 02再レンダーを抑える状態選択
  3. 03コンポーネント外からの参照と購読
  4. 04Reactを使わないバニラストア
  5. 05persistとdevtoolsの安全要件確認

活用シーン

  1. 01Reactアプリの状態管理
  2. 02コンポーネントで使う状態の選択
  3. 03ストアと外部システムの連携
  4. 04永続化とDevTools設定のレビュー

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/existential-birds/beagle --skill zustand-state

インストール後、Claude Code で/zustand-stateを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

MIDDLEWARE.md
PATTERNS.md
TYPESCRIPT.md
references/
middleware.md
patterns.md
typescript.md

詳細

Zustand の状態管理

最小限の状態管理です。プロバイダーは不要で、ボイラープレートも最小限です。

クイックリファレンス

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

Vanilla ストア(React なし)

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))

追加ドキュメント

  • ミドルウェア:persist、devtools、immer については references/middleware.md を参照してください
  • パターン:スライス、テスト、ベストプラクティスについては references/patterns.md を参照してください
  • TypeScript:高度な型付けパターンについては references/typescript.md を参照してください

主なパターン

パターン使用する場面
単一のセレクター必要な状態が 1 つの場合
useShallow複数の値を取得し、再レンダリングを避けたい場合
getState()React の外部やイベントハンドラーで使用する場合
subscribe()外部システムとの連携やログ記録に使用する場合
Vanilla ストアReact 以外の環境で使用する場合

ゲート(persist と devtools)

これらのミドルウェアを使用するコードをマージする前に、次の点を確認してください(セットアップについては references/middleware.md を参照してください)。

  1. persist — 合格条件: 永続化するキーが明示されていること(partialize を使用するか、レビュー済みの全状態スナップショットを使用すること)。また、ストレージに書き込まれる JSON に認証トークン、API キー、その他のシークレットが含まれないこと。
  2. リリースバンドルでの devtools — 合格条件: DevTools が開発環境でのみ実行される(環境変数によるゲート制御がある)こと。または、本番環境でも有効にしておく理由を説明する 1 行のメモを PR に残すこと。

コメント