Pinia Setup
Pinia is initialized in the application entry point:src/main.js
Favoritos Store
Thefavoritos store manages the user’s favorite Pokemon list:
src/stores/favoritos.js
Store Structure
State
State
Using the Store in Components
The store is used in components through the Composition API:Basic Usage
src/views/PokeView.vue
Important: storeToRefs
Always use
storeToRefs() when destructuring state properties to preserve reactivity. Direct destructuring will lose reactivity!Adding to Favorites
src/views/PokeView.vue
Checking if Pokemon is Favorite
src/views/PokeView.vue
Removing from Favorites
Setup Syntax vs Options API
This store uses Pinia’s Setup Syntax (recommended for Vue 3):State Persistence
Current Limitation
The current implementation does not persist favorites across page refreshes. Favorites are stored only in memory.To add persistence, consider using:
- localStorage via Pinia plugins
- sessionStorage for session-only persistence
- Backend API for cross-device synchronization
Example: Adding Persistence
Best Practices
Use storeToRefs for State
Use storeToRefs for State
Always destructure state with
storeToRefs() to maintain reactivity:Direct Action Destructuring
Direct Action Destructuring
Actions can be destructured directly without losing reactivity:
Single Store Instance
Single Store Instance
Store instances are automatically singletons. Multiple calls to
useFavoritosStore() return the same instance:Validate Before Mutating
Validate Before Mutating
Always validate data before mutating the store:
Store DevTools
Vue DevTools Integration
Pinia integrates with Vue DevTools for debugging:
- View current state
- Track state mutations
- Time-travel debugging
- Action history
Next Steps
Composables
Learn about reusable composition functions
Routing
Understand route configuration and navigation