This document outlines the Pinia state management patterns used in this Nuxt 4 project.
Implementation: See app/stores/usePreferences.ts for complete store definition with state, actions, and persistence configuration.
Implementation: See app/composables/usePreferences.ts for composable wrapper pattern with reactive refs.
Configuration: See nuxt.config.ts for Pinia modules configuration with persistedstate plugin.
Implementation: See app/stores/usePreferences.ts for cookie storage configuration with expiration and security options.
Implementation: See app/components/layout/PreferencesControls.vue for complete component usage with Pinia store integration.
Implementation: See shared/types/preferences.ts for PostViewMode and PostDisplayMode type definitions.
- One store per domain/feature
- Keep stores focused and cohesive
- Use descriptive store names with
useprefix
- Prefer explicit actions over direct state mutation
- Use meaningful action names that describe intent
- Keep actions simple and focused
- Clean separation between store and component logic
- Easier testing and mocking
- Consistent API across components
- Better TypeScript inference
- Use cookies for user preferences (7-day expiration)
- Consider localStorage for larger data sets
- Use sessionStorage for temporary state
- Implement proper error handling for storage failures
Implementation: Store testing patterns can be implemented using createPinia setup and component mocking.