Skip to main content

State Management (Pinia)

MStore Dashboard menggunakan Pinia 3.x sebagai state management solution dengan pendekatan composition API.

Overview

State Architecture

Three Levels of State

Decision Matrix

Setup

Installation

Pinia sudah terinstall via @pinia/nuxt module:

Persistence Plugin

Untuk persistent state, gunakan pinia-plugin-persistedstate:

Store Patterns

Basic Store (Composition API)

Auth Store with Persistence

Feature Store with API Integration

Composable Patterns

Page Composable

Form Composable

Using in Components

In Page

In Component

Persistence

Full Store Persistence

Partial Persistence

Custom Storage

Best Practices

  • One store per domain
  • Keep stores focused and small
  • Use composables for page-specific logic
  • Use computed for derived state
  • Never persist sensitive data (tokens, passwords)
  • Use memory-only for access tokens
  • Validate data before storing
  • Clear sensitive data on logout
  • Use shallowRef for large arrays
  • Avoid deep watchers on store state
  • Use computed instead of methods for derived data
  • Batch updates when possible

Next Steps

Data Flow

Complete data flow patterns

API Integration

API fetching dengan auto-refresh