Pinia
Pinia is Vue's official store: option or setup stores with state, cached getters and actions, no mutations, plus plugins and SSR hydration. Interviewers compare it with Vuex.
on this pageshowhide
explore
- Defining Stores5 questions
- Patching & Subscribing6 questions
- Store Getters5 questions
- Actions & Action Hooks5 questions
- Store Plugins5 questions
- SSR & Non-Component Use5 questions
- Unit-Testing Store Logic5 questions
- Moving From Vuex5 questions
questions
page 2 of 2In a hand-rolled SSR storefront using Pinia, how do you ship pinia.state.value to the browser, and why must it be restored before any store is used?
basics
~20 sAfter rendering, serialise pinia.state.value with an escaping serialiser into the page; on the client, assign it back to pinia.state.value right after app.use(pinia), before any store call, because stores read their initial state only when created.
A Pinia $subscribe audit log set up in a preferences panel misses changes after the panel closes, and a write made right after $patch; why, and what fixes each?
basics
~20 sA $subscribe made inside a component is removed when it unmounts unless { detached: true } is passed. And $patch pauses the watcher until the next tick, so a direct write beside it gets no non-sync call; flush: 'sync' reports every write.
With @pinia/testing, how do you start a toast component test with three unread notifications and force the notifications store's unreadCount getter?
basics
~20 sPass initialState: { notifications: { items: [...] } } to createTestingPinia, keyed by the store id; it is merged into the store when created. Force a getter by assigning store.unreadCount = 9, and assign undefined to restore it.
In a Pinia setup store, notifyError() calls push() directly; why does a test's stub of push never see that call, and what can you do?
basics
~20 screateTestingPinia replaces actions on the store object, but inside a setup store notifyError() calls the local push function it closed over, not store.push. The real push runs; option stores avoid this because this.push goes through the store.
Moving a Vue 3 app's namespaced Vuex modules (auth, cart, catalog) to Pinia one store at a time, how do you convert cart while auth still lives in Vuex?
basics
~20 sInstall Pinia beside Vuex, rewrite cart as defineStore('cart', …) with a state function, mutations folded into actions and context replaced by this, read auth from the Vuex store instance until it moves, then switch cart's callers and delete the Vuex module.
In a Pinia setup store for checkout, what happens to a composable's watchers, lifecycle hooks and inject() calls, and what does the store expose from it?
basics
~20 sA composable's watchers and computeds join the store's effect scope and live until the store is disposed. Component lifecycle hooks do not follow the store, and inject() sees only app-level provides. Returned refs become state, functions actions, computeds getters.
In a Vite app, what does Pinia's acceptHMRUpdate(useCartStore, import.meta.hot) preserve when you edit the cart store file, and what happens without it?
basics
~20 sacceptHMRUpdate keeps the live cart store and its current state while swapping in edited actions and getters and adding or removing state keys. Without it, an edit usually reloads the page, emptying the cart, or leaves the old store code running.
In a server-rendered Pinia setup store, a recently-viewed list read from localStorage is wiped on hydration; why, and what does skipHydrate() change?
basics
~20 sDuring client hydration a setup store copies the server's value into each returned ref, so the server's empty list overwrites the browser's. skipHydrate() on that ref makes Pinia skip it; option stores use a hydrate() option instead.
In a Pinia option store, what does store.$state = savedPrefs actually do to the existing state, and how does it differ from $patch(savedPrefs)?
basics
~20 sAssigning $state keeps the same state object and runs a function $patch that Object.assigns each top-level key: nested objects are replaced whole, absent keys keep their values. $patch(savedPrefs) deep-merges plain objects and reports a payload.
A Pinia notifications store depends on a store plugin; how do you make that plugin apply in tests, and why not call testingPinia.use()?
basics
~20 sPlugins added with pinia.use() only run once the pinia is installed in an app. For a plain pinia, install it into createApp({}); with createTestingPinia, pass the plugin in its plugins option so it runs before the action stubs.
In Options API components moving from Vuex to Pinia, how do mapStores, mapState, mapWritableState and mapActions replace Vuex's map helpers?
basics
~10 sSpread mapStores, mapState and mapWritableState into computed and mapActions into methods, passing the store's useStore function instead of a Vuex namespace string. mapState is read-only, mapWritableState adds setters, and nothing maps mutations.
showing 31–41 of 41