jpskill.com
🛠️ 開発・MCP コミュニティ

web-state-pinia

Vue 3 アプリケーションでクライアントの状態を管理する際に、Pinia ストアのオプションやセットアップストアを選択したり、ストアを組み合わせたり、データの永続化を実装したりするのに役立つSkill。

📜 元の英語説明(参考)

Pinia stores, Vue 3 state patterns. Use when managing client state in Vue applications, choosing between Options/Setup stores, composing stores, or implementing persistence.

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

一言でいうと

Vue 3 アプリケーションでクライアントの状態を管理する際に、Pinia ストアのオプションやセットアップストアを選択したり、ストアを組み合わせたり、データの永続化を実装したりするのに役立つSkill。

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

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

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

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

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

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して web-state-pinia.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → web-state-pinia フォルダができる
  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
同梱ファイル
1

📖 Skill本文(日本語訳)

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

Pinia State Management Patterns

クイックガイド: Vue 3 のすべての共有クライアント状態には Pinia を使用してください。シンプルさには Options store、柔軟性には Setup store を使用します。サーバーデータですか?データ取得ソリューションを使用してください。状態を分割代入するときは storeToRefs() を使用してください。


<critical_requirements>

重要: Pinia で状態を管理する前に

(すべてのサーバー/API データには、データ取得ソリューションを必ず使用してください - API レスポンスを Pinia ストアに絶対に入れないでください)

(ストアから状態を分割代入するときは、必ず storeToRefs() を使用してください - 直接分割代入するとリアクティビティが失われます)

(Setup store では、すべての状態プロパティを必ず返してください - プライベートな状態は SSR と DevTools を壊します)

(必ず名前付きエクスポートのみを使用してください - ストアファイルにはデフォルトエクスポートを使用しないでください)

(すべての数値には、必ず名前付き定数を使用してください - 状態コードにマジックナンバーを使用しないでください)

</critical_requirements>


自動検出: Pinia, defineStore, Vue 3 state, storeToRefs, Vue state management, Options store, Setup store

使用する場合:

  • Vue 3 アプリケーションで共有クライアント状態を管理する場合
  • Options store と Setup store のどちらかを選択する場合
  • 互いに依存するストアを構成する場合
  • セッション間で状態の永続性を実装する場合
  • デバッグのために DevTools 統合を追加する場合
  • SSR アプリケーション用にストアをセットアップする場合

使用しない場合:

  • サーバー/API データ (専用のデータ取得ソリューションを使用)
  • 単純なコンポーネントローカル状態 (ref() または reactive() を使用)
  • フィルターのような URL に適した状態 (ルートクエリパラメータを使用)

<philosophy>

Philosophy

Pinia は Vue 3 の公式ステート管理ソリューションであり、直感的でタイプセーフ、かつ柔軟になるように設計されています。DevTools 統合、プラグインサポート、SSR 互換性などの強力な機能を維持しながら、Vuex のボイラープレートを排除します。

コア原則:

  1. デフォルトでモジュール式 - 各ストアは独立しており、ネストされたモジュールはありません
  2. ミューテーションなし - アクションはすべての状態変更を直接処理します
  3. 完全な TypeScript サポート - 型推論はすぐに機能します
  4. Composition API フレンドリー - <script setup> とシームレスに連携します

状態の所有権:

State Type Solution Reason
Server/API data Data fetching solution キャッシュ、同期、ローディング状態
Shared client state Pinia リアクティビティ、DevTools、永続性
Component-local state ref() / reactive() よりシンプル、オーバーヘッドなし
URL state (filters) Route query params 共有可能、ブックマーク可能

</philosophy>


<patterns>

コアパターン

パターン 1: Options Store と Setup Store の選択

Pinia は 2 つのストア定義構文を提供します。ニーズに基づいて選択してください。

決定木

どのストア構文を使用すべきですか?

コンポーザブル (useRoute, useI18n) が必要ですか?
├─ YES → Setup Store
└─ NO → ストア内でウォッチャーが必要ですか?
    ├─ YES → Setup Store
    └─ NO → Vue Options API スタイルが好きですか?
        ├─ YES → Options Store
        └─ NO → Setup Store (より柔軟)

Options Store: よりシンプル、Vuex ユーザーには馴染みがある、組み込みの $reset() メソッド Setup Store: より柔軟、コンポーザブルをサポート、手動リセットが必要

実装例については、examples/core.md を参照してください。


パターン 2: Options Store の定義

使い慣れた構文で簡単な状態管理を行うには、Options store を使用します。

使用する場合

  • Vuex または Vue Options API に精通しているチーム
  • コンポーザブルな依存関係のないシンプルなストア
  • 組み込みの $reset() メソッドが必要な場合
  • クライアント状態に対する標準的な CRUD 操作

実装例と良い/悪い比較については、examples/core.md を参照してください。


パターン 3: Setup Store の定義

最大限の柔軟性とコンポーザブルな統合が必要な場合は、Setup store を使用します。

使用する場合

  • Vue Router コンポーザブル (useRoute, useRouter) を使用する場合
  • i18n コンポーザブル (useI18n) を使用する場合
  • ストア内でウォッチャーが必要な場合
  • 複雑な算出プロパティの依存関係
  • 外部コンポーザブルとの統合

Setup Store の重要な要件

  • すべての状態プロパティを返す (プライベートな状態は不可)
  • 状態には ref()、ゲッターには computed() を使用する
  • 関数は自動的にアクションになる
  • 必要に応じてカスタム $reset() を実装する必要がある

実装例については、examples/core.md を参照してください。


パターン 4: コンポーネントでのストア状態へのアクセス

適切なストアアクセスパターンは、リアクティビティの問題を防ぎます。

主要なルール

  1. 状態を直接分割代入しない - リアクティビティが失われる
  2. 状態/ゲッターには storeToRefs() を使用する - リアクティビティが保持される
  3. アクションは直接分割代入する - リアクティビティは必要ない

storeToRefs を使用した実装例については、examples/core.md を参照してください。


パターン 5: ストアの構成

ストアは他のストアを使用できますが、循環依存を避けるためにルールに従ってください。

ガイドライン

  • ストア関数の先頭でストアをインポートして使用する
  • ゲッター/アクションを介した循環依存を避ける
  • ストアが互いに参照している場合は、無限ループが発生しないようにする
  • 複雑な構成パターンには Setup store を使用する

実装例については、examples/core.md を参照してください。


パターン 6: 状態の永続化

セッション間で状態を永続化するには、プラグインを使用します。

永続化のガイドライン

  • ほとんどの場合、pinia-plugin-persistedstate を使用する
  • ユーザー設定と機密性の低いデータのみを永続化する
  • 特定のプロパティのみを永続化するには、pick オプションを使用する
  • サーバーデータを永続化しない (代わりにロード時に再フェッチする)
  • ストレージタイプを検討する: localStorage vs sessionStorage

実装例については、examples/persistence.md を参照してください。


パターン 7: Pinia プラグイン

共有機能ですべてのストアを拡張します。

一般的なプラグインのユースケース

  • 共有プロパティ (router, analytics) の追加
  • ロギング/タイミングによるアクションのラップ
  • カスタムオプション (debounce, throttle) の追加
  • si

(原文がここで切り詰められています)

📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Pinia State Management Patterns

Quick Guide: Use Pinia for all shared client state in Vue 3. Options stores for simplicity, Setup stores for flexibility. Server data? Use your data fetching solution. Use storeToRefs() when destructuring state.


<critical_requirements>

CRITICAL: Before Managing State with Pinia

(You MUST use a data fetching solution for ALL server/API data - NEVER put API responses in Pinia stores)

(You MUST use storeToRefs() when destructuring state from stores - direct destructuring loses reactivity)

(You MUST return ALL state properties in Setup stores - private state breaks SSR and DevTools)

(You MUST use named exports ONLY - NO default exports in any store files)

(You MUST use named constants for ALL numbers - NO magic numbers in state code)

</critical_requirements>


Auto-detection: Pinia, defineStore, Vue 3 state, storeToRefs, Vue state management, Options store, Setup store

When to use:

  • Managing shared client state in Vue 3 applications
  • Choosing between Options stores and Setup stores
  • Composing stores that depend on each other
  • Implementing state persistence across sessions
  • Adding DevTools integration for debugging
  • Setting up stores for SSR applications

When NOT to use:

  • Server/API data (use a dedicated data fetching solution)
  • Simple component-local state (use ref() or reactive())
  • URL-appropriate state like filters (use route query params)

<philosophy>

Philosophy

Pinia is the official state management solution for Vue 3, designed to be intuitive, type-safe, and flexible. It eliminates the boilerplate of Vuex while maintaining powerful features like DevTools integration, plugin support, and SSR compatibility.

Core Principles:

  1. Modular by Default - Each store is independent, no nested modules
  2. No Mutations - Actions handle all state changes directly
  3. Full TypeScript Support - Type inference works out of the box
  4. Composition API Friendly - Works seamlessly with <script setup>

State Ownership:

State Type Solution Reason
Server/API data Data fetching solution Caching, synchronization, loading states
Shared client state Pinia Reactivity, DevTools, persistence
Component-local state ref() / reactive() Simpler, no overhead
URL state (filters) Route query params Shareable, bookmarkable

</philosophy>


<patterns>

Core Patterns

Pattern 1: Options Store vs Setup Store Decision

Pinia offers two store definition syntaxes. Choose based on your needs.

Decision Tree

Which store syntax should I use?

Need composables (useRoute, useI18n)?
├─ YES → Setup Store
└─ NO → Need watchers inside store?
    ├─ YES → Setup Store
    └─ NO → Prefer Vue Options API style?
        ├─ YES → Options Store
        └─ NO → Setup Store (more flexible)

Options Store: Simpler, familiar to Vuex users, built-in $reset() method Setup Store: More flexible, supports composables, requires manual reset

For implementation examples, see examples/core.md.


Pattern 2: Options Store Definition

Use Options stores for straightforward state management with familiar syntax.

When to Use

  • Teams familiar with Vuex or Vue Options API
  • Simple stores without composable dependencies
  • When built-in $reset() method is needed
  • Standard CRUD operations on client state

For implementation examples and good/bad comparisons, see examples/core.md.


Pattern 3: Setup Store Definition

Use Setup stores when you need maximum flexibility and composable integration.

When to Use

  • Using Vue Router composables (useRoute, useRouter)
  • Using i18n composables (useI18n)
  • Need watchers inside the store
  • Complex computed dependencies
  • Integrating with external composables

Critical Requirements for Setup Stores

  • Return ALL state properties (no private state)
  • Use ref() for state, computed() for getters
  • Functions become actions automatically
  • Must implement custom $reset() if needed

For implementation examples, see examples/core.md.


Pattern 4: Accessing Store State in Components

Proper store access patterns prevent reactivity issues.

Key Rules

  1. Never destructure state directly - loses reactivity
  2. Use storeToRefs() for state/getters - preserves reactivity
  3. Destructure actions directly - they don't need reactivity

For implementation examples with storeToRefs, see examples/core.md.


Pattern 5: Composing Stores

Stores can use other stores, but follow rules to avoid circular dependencies.

Guidelines

  • Import and use stores at the top of your store function
  • Avoid circular dependencies through getters/actions
  • If stores reference each other, ensure no infinite loops
  • Use setup stores for complex composition patterns

For implementation examples, see examples/core.md.


Pattern 6: State Persistence

Use plugins for persisting state across sessions.

Persistence Guidelines

  • Use pinia-plugin-persistedstate for most cases
  • Only persist user preferences and non-sensitive data
  • Use pick option to persist specific properties only
  • Never persist server data (refetch on load instead)
  • Consider storage type: localStorage vs sessionStorage

For implementation examples, see examples/persistence.md.


Pattern 7: Pinia Plugins

Extend all stores with shared functionality.

Common Plugin Use Cases

  • Adding shared properties (router, analytics)
  • Wrapping actions with logging/timing
  • Adding custom options (debounce, throttle)
  • Implementing side effects (localStorage)

For implementation examples, see examples/plugins.md.


Pattern 8: Testing Stores

Test stores in isolation and within components.

Testing Approaches

  1. Unit tests: Test store actions/getters directly with setActivePinia()
  2. Component tests: Use createTestingPinia() for component mounting
  3. Mocking: Actions are stubbed by default with @pinia/testing

For implementation examples, see examples/testing.md.


Pattern 9: SSR Considerations

Handle server-side rendering and hydration properly.

SSR Guidelines

  • Use SSR-safe defaults (no browser APIs in initial state)
  • Guard client-only logic with typeof window !== "undefined" checks
  • Be careful with Setup stores - ensure all refs are returned
  • Sanitize serialized state to prevent XSS

For implementation examples, see examples/ssr.md.

</patterns>


<red_flags>

RED FLAGS

High Priority Issues:

  • Storing server/API data in Pinia instead of using a data fetching solution - causes stale data, no caching, manual sync
  • Destructuring state without storeToRefs() - silently breaks reactivity, UI shows stale data
  • Private state in Setup stores (not returning all refs) - breaks SSR hydration, DevTools, and plugins
  • Browser APIs in state initialization (localStorage, window) - crashes SSR

Gotchas & Edge Cases:

  • storeToRefs() works for state and getters only, not actions - destructure actions directly
  • Options stores have built-in $reset(), Setup stores do not - implement manually or use a plugin
  • Store instances are singletons per Pinia instance - in SSR, each request needs its own Pinia
  • $patch with a function allows accessing current state: store.$patch(state => { state.items.push(item) })
  • Arrow functions in Options store getters have no this - use function keyword or state parameter

For complete anti-pattern examples, see reference.md.

</red_flags>


Detailed Resources:


<critical_reminders>

CRITICAL REMINDERS

(You MUST use a data fetching solution for ALL server/API data - NEVER put API responses in Pinia stores)

(You MUST use storeToRefs() when destructuring state from stores - direct destructuring loses reactivity)

(You MUST return ALL state properties in Setup stores - private state breaks SSR and DevTools)

(You MUST use named exports ONLY - NO default exports in any store files)

(You MUST use named constants for ALL numbers - NO magic numbers in state code)

Failure to follow these rules will cause reactivity bugs, SSR hydration errors, and DevTools failures.

</critical_reminders>