Vue.js Assignment– 4
State Management with Vuex & Pinia
Basic Questions
- Create a Vuex store with state: { count: 0 }, a mutation to increment, and render the count in a component.
- Add a Vuex getter doubleCount and display it in the UI.
- Dispatch a Vuex action that commits the increment mutation after 500ms.
- Split the store into a user module with state: { name: ” } and a setName mutation.
- Enable Vuex strict mode and demonstrate a console error when mutating state outside mutations.
- Create a namespaced cart module and read a getter using its namespace.
- Map Vuex state, getters, actions, and mutations in a component using helpers (mapState, etc.).
- Add a products module that fetches a JSON list (mock with setTimeout) via an action and stores it.
- Persist Vuex user.name to localStorage on mutation and hydrate it on app load.
- Persist the cart module to sessionStorage and restore it on refresh.
- Add a plugin that logs every mutation type and payload to the console.
- Create a Vuex action that handles API errors and stores an error message in state.
- Add a root-level RESET_STATE mutation that clears all modules’ state.
- Move hard-coded feature flags into Vuex and read them via getters in multiple components.
- Implement a logout action that clears tokens in state and storage.
- Add a derived getter that filters products by a search term in state.
- Lazy-register the cart module only when the cart page is visited.
- Unregister the cart module when leaving the cart page and verify state is removed.
- Create a loading boolean in state and toggle it around async actions.
- Document the difference between mutations and actions with a small code example.
Intermediate Questions
- Organize Vuex into namespaced modules: auth, cart, catalog, each with state/getters/mutations/actions.
- Implement cross-module communication: an auth/login action commits cart/mergeGuestCart with payload.
- Add a Vuex plugin that automatically persists selected module keys to localStorage (whitelist).
- Implement optimistic updates: immediately add an item to cart, revert on API failure.
- Use a factory function to create initial state to avoid shared references across modules.
- Add a memoized getter that computes cart total and verify it doesn’t recompute unnecessarily.
- Guard against committing large payloads by normalizing entities (by id) in mutations.
- Implement action cancellation (ignore late responses) with a request token stored in state.
- Add a checkout action that sequences multiple async calls and commits progress steps.
- Create a module with dynamic namespace registered per route parameter (e.g., board/:id).
- Add SSR-friendly hydration logic for Vuex state (simulate with pre-injected window state).
- Enforce strict mode only in development and measure perf impact in production.
- Replace direct localStorage writes with a storage service layer used by Vuex plugins.
- Add rehydration versioning: if stored schema version mismatches, run a migration before loading.
- Implement time-based expiration for persisted keys and clear them when stale.
- Create a namespaced notifications module with queue behavior (enqueue/dequeue mutations).
- Batch multiple related mutations into a single action and commit them atomically.
- Add unit tests for a module’s mutations and actions using a test runner.
- Profile a large mutation payload and refactor to incremental patch mutations.
- Document a module communication diagram showing action flows and mutation effects.
Advanced Questions
- Migrate an existing Vuex store to Pinia: create equivalent stores, actions, and getters with defineStore.
- Replace Vuex helpers with Pinia composables (useXStore) in components and verify reactivity.
- Implement a Pinia plugin to persist selected store state to localStorage with versioning and TTL.
- Convert Vuex modules to separate Pinia stores and model cross-store interactions (e.g., auth ↔ cart).
- Use Pinia’s store.$subscribe to log mutations-like patches and sync to storage efficiently.
- Implement a Pinia action with optimistic update + rollback on failure and verify UI consistency.
- Benchmark Vuex vs Pinia update paths on a list of 5k items and report render timings.
- Build a large-scale pattern: domain stores (entities), UI stores (view state), and session store; enforce read-only accessors and typed actions.
- Create a storage abstraction that supports localStorage, sessionStorage, and an in-memory fallback for SSR, and plug it into both Vuex and Pinia persistence.
- Final Hands-on Project:
- Start with a Vuex app using namespaced modules, strict mode, plugins, and persisted subsets
- Migrate fully to Pinia with equivalent stores and custom persistence plugin
- Apply best practices for large-scale apps (normalized state, action sequencing, error handling, SSR-safe hydration)
- Prove performance gains with measurements and explain trade-offs.