jpskill.com
📦 その他 コミュニティ

superpower-zustand

Zustandを使った状態管理やストア作成を依頼された際に、immerミドルウェアとファクトリーパターン分離を用いたStoreBuilderパターンでストアを構築する必要があり、Zustandストア作成に必須となるSkill。

📜 元の英語説明(参考)

MANDATORY for creating Zustand stores. This skill is required when users request state management, creating stores, or mention Zustand. Do NOT create Zustand stores without this skill - all stores must use the required StoreBuilder pattern with immer middleware and factory pattern separation

🇯🇵 日本人クリエイター向け解説

一言でいうと

Zustandを使った状態管理やストア作成を依頼された際に、immerミドルウェアとファクトリーパターン分離を用いたStoreBuilderパターンでストアを構築する必要があり、Zustandストア作成に必須となるSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o superpower-zustand.zip https://jpskill.com/download/18244.zip && unzip -o superpower-zustand.zip && rm superpower-zustand.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/18244.zip -OutFile "$d\superpower-zustand.zip"; Expand-Archive "$d\superpower-zustand.zip" -DestinationPath $d -Force; ri "$d\superpower-zustand.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して superpower-zustand.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → superpower-zustand フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
3

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Zustand StoreBuilder パターン

<CRITICAL> 標準的なパターン(インラインアクションによる作成)を使用して Zustand ストアを作成しないでください。このプロジェクトのすべての Zustand ストアは、以下に定義する StoreBuilder パターンを使用する必要があります。これは必須のアーキテクチャ標準であり、提案ではありません。 </CRITICAL>

目的

Zustand ストアの作成に対する標準化された、タイプセーフなアプローチを強制します。

  • ファクトリパターンを使用して、状態の定義とアクションを分離する
  • 便利なイミュータブルな更新のために immer ミドルウェアを統合する
  • きめ細かい制御によるオプションの永続化をサポートする
  • リアクティブ(useStore hook)および非リアクティブ(get/set)アクセスの両方を公開する
  • コードベース全体で一貫したパターンを維持する

この Skill を使用するタイミング

この Skill は、以下の場合に使用します。

  • 状態管理のために新しい Zustand ストアを作成する場合
  • ユーザーが React アプリケーションで状態管理ソリューションを要求する場合
  • クライアント側の状態を必要とする機能のためにストアを実装する場合

必須パターン

すべての Zustand ストアは、assets/storebuilder.ts にある StoreBuilder ユーティリティを使用する必要があります。

コア実装手順

  1. StoreBuilder ユーティリティをコピーする(プロジェクトにまだない場合)

    • ソース: skills/superpower-zustand/assets/storebuilder.ts
    • 宛先: src/lib/storebuilder.ts (またはプロジェクト内の同様の場所)
  2. 状態の型をアクションとは別に定義する

    • 完全なストア(状態 + アクション)の型を作成する
    • StoreBuilder に渡すときに、Omit を使用してアクションメソッドを除外する
  3. StoreBuilder でストアを初期化する

    • 最初の引数として初期状態を渡す
    • オプションで、永続化のために 2 番目の引数として PersistConfig を渡す
  4. createFactory を使用してアクションを分離する

    • すべてのアクションを createFactory 引数内のメソッドとして定義する
    • アクションは StoreBuilder クロージャから set にアクセスする
    • set コールバック内で immer スタイルのミューテーションを使用する
  5. ファクトリによって作成されたフックをエクスポートする

    • createFactory によって返されるフックは、状態、アクション、およびストアユーティリティを組み合わせる

必須のコード構造

import { StoreBuilder } from './storebuilder';

// 1. 完全な状態の型を定義する
type MyStoreState = {
  // 状態フィールド
  value: number;
  items: string[];

  // アクションメソッド
  setValue: (v: number) => void;
  addItem: (item: string) => void;
};

// 2. 状態のみで StoreBuilder を初期化する (アクションを Omit する)
const { set, createFactory } = StoreBuilder<Omit<MyStoreState, 'setValue' | 'addItem'>>(
  {
    value: 0,
    items: [],
  },
  // オプション: 永続化設定
  // {
  //   name: 'my-store',
  //   version: 1,
  // }
);

// 3. アクションでファクトリを作成する
const useMyStore = createFactory({
  setValue: (v: number) => set((state) => { state.value = v; }),
  addItem: (item: string) => set((state) => { state.items.push(item); }),
});

// 4. フックをエクスポートする
export { useMyStore };

Immer による状態の更新

set を使用する場合、ドラフト状態に直接ミューテーションを記述します(immer ミドルウェアが含まれています)。

// ✅ 正しい: ドラフトをミューテートする
set((state) => {
  state.count += 1;
  state.items.push(newItem);
  state.nested.property = 'value';
});

// ❌ 間違い: 新しいオブジェクトを返さない
set((state) => ({ ...state, count: state.count + 1 }));

永続化設定

状態をセッション間で永続化する必要がある場合:

const { createFactory } = StoreBuilder(
  initialState,
  {
    name: 'storage-key',           // 必須: localStorage キー
    version: 1,                     // オプション: 移行処理用
    storage: sessionStorage,        // オプション: デフォルトは localStorage
    partialize: (state) => ({       // オプション: 特定のフィールドのみを永続化する
      theme: state.theme,
      preferences: state.preferences,
    }),
  }
);

参照ドキュメント

詳細な例と高度なパターンについては、references/pattern-guide.md を参照してください。

  • 基本的な使用例
  • 永続化パターン
  • 非同期アクションを含む複雑なストア
  • React コンポーネントの外部での get/set の使用
  • 型安全パターン

以下の場合に参照ドキュメントをロードします。

  • 非同期操作を含む複雑なストアを実装する場合
  • 永続化設定の例が必要な場合
  • ユーザーが高度な Zustand パターンについて質問する場合
  • 特定の実装の詳細が不明な場合

検証

ストアを作成した後、以下を確認します。

  1. ✅ StoreBuilder ユーティリティがプロジェクトの場所からインポートされている
  2. ✅ 状態の型が Omit を使用してアクションを除外している
  3. ✅ すべてのアクションが初期状態ではなく、createFactory で定義されている
  4. ✅ 状態の更新が immer スタイルのミューテーションを使用している(ドラフトをミューテートし、新しいオブジェクトを返さない)
  5. ✅ エクスポートされたフック名が規則に従っている(例:useMyStore
  6. ✅ 状態を永続化する必要がある場合は、永続化設定が含まれている

React 以外の使用法

このパターンは、React コンポーネントの外部での非リアクティブアクセスをサポートしています。

const { get, set, subscribe } = StoreBuilder(initialState);

// 現在の状態を取得する
const current = get();

// 状態を更新する
set((state) => { state.value = 10; });

// 変更をサブスクライブする
const unsubscribe = subscribe((state) => console.log(state));

以下の場合に get および set を使用します。

  • ユーティリティ関数で状態にアクセスする場合
  • ミドルウェアまたは副作用を実装する場合
  • React コンポーネントのライフサイクルの外部で作業する場合
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Zustand StoreBuilder Pattern

<CRITICAL> DO NOT create Zustand stores using standard patterns (create with inline actions). ALL Zustand stores in this project MUST use the StoreBuilder pattern defined below. This is a required architectural standard, not a suggestion. </CRITICAL>

Purpose

Enforce a standardized, type-safe approach to creating Zustand stores that:

  • Separates state definition from actions using the factory pattern
  • Integrates immer middleware for convenient immutable updates
  • Supports optional persistence with fine-grained control
  • Exposes both reactive (useStore hook) and non-reactive (get/set) access
  • Maintains consistent patterns across the codebase

When to Use This Skill

Use this skill when:

  • Creating new Zustand stores for state management
  • User requests state management solutions in a React application
  • Implementing stores for any feature requiring client-side state

Required Pattern

All Zustand stores MUST use the StoreBuilder utility located in assets/storebuilder.ts.

Core Implementation Steps

  1. Copy the StoreBuilder utility (if not already in the project)

    • Source: skills/superpower-zustand/assets/storebuilder.ts
    • Destination: src/lib/storebuilder.ts (or similar location in the project)
  2. Define state type separately from actions

    • Create a type for the full store (state + actions)
    • Use Omit to exclude action methods when passing to StoreBuilder
  3. Initialize the store with StoreBuilder

    • Pass initial state as first argument
    • Optionally pass PersistConfig as second argument for persistence
  4. Separate actions using createFactory

    • Define all actions as methods in the createFactory argument
    • Actions access set from the StoreBuilder closure
    • Use immer-style mutations within set callbacks
  5. Export the factory-created hook

    • The hook returned by createFactory combines state, actions, and store utilities

Required Code Structure

import { StoreBuilder } from './storebuilder';

// 1. Define complete state type
type MyStoreState = {
  // State fields
  value: number;
  items: string[];

  // Action methods
  setValue: (v: number) => void;
  addItem: (item: string) => void;
};

// 2. Initialize StoreBuilder with state only (Omit actions)
const { set, createFactory } = StoreBuilder<Omit<MyStoreState, 'setValue' | 'addItem'>>(
  {
    value: 0,
    items: [],
  },
  // Optional: persistence config
  // {
  //   name: 'my-store',
  //   version: 1,
  // }
);

// 3. Create factory with actions
const useMyStore = createFactory({
  setValue: (v: number) => set((state) => { state.value = v; }),
  addItem: (item: string) => set((state) => { state.items.push(item); }),
});

// 4. Export the hook
export { useMyStore };

State Updates with Immer

When using set, write mutations directly on the draft state (immer middleware is included):

// ✅ Correct: Mutate draft
set((state) => {
  state.count += 1;
  state.items.push(newItem);
  state.nested.property = 'value';
});

// ❌ Incorrect: Don't return new object
set((state) => ({ ...state, count: state.count + 1 }));

Persistence Configuration

When state should persist across sessions:

const { createFactory } = StoreBuilder(
  initialState,
  {
    name: 'storage-key',           // Required: localStorage key
    version: 1,                     // Optional: for migration handling
    storage: sessionStorage,        // Optional: defaults to localStorage
    partialize: (state) => ({       // Optional: persist only specific fields
      theme: state.theme,
      preferences: state.preferences,
    }),
  }
);

Reference Documentation

For detailed examples and advanced patterns, read references/pattern-guide.md:

  • Basic usage examples
  • Persistence patterns
  • Complex stores with async actions
  • Using get/set outside React components
  • Type safety patterns

Load the reference documentation when:

  • Implementing complex stores with async operations
  • Needing examples of persistence configuration
  • User asks about advanced Zustand patterns
  • Unsure about specific implementation details

Verification

After creating a store, verify:

  1. ✅ StoreBuilder utility is imported from project location
  2. ✅ State type uses Omit to exclude actions
  3. ✅ All actions are defined in createFactory, not in initial state
  4. ✅ State updates use immer-style mutations (mutate draft, don't return new object)
  5. ✅ Exported hook name follows convention (e.g., useMyStore)
  6. ✅ Persistence config is included if state should persist

Non-React Usage

The pattern supports non-reactive access outside React components:

const { get, set, subscribe } = StoreBuilder(initialState);

// Get current state
const current = get();

// Update state
set((state) => { state.value = 10; });

// Subscribe to changes
const unsubscribe = subscribe((state) => console.log(state));

Use get and set when:

  • Accessing state in utility functions
  • Implementing middleware or side effects
  • Working outside React component lifecycle

同梱ファイル

※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。