skip to content

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 pageshow

explore

questions

page 2 of 2

In 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?

level: seniorimportance: should knowfreq 43%

basics

~20 s

After 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.

open as a page

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?

level: seniorimportance: should knowfreq 30%

basics

~20 s

A $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.

open as a page

With @pinia/testing, how do you start a toast component test with three unread notifications and force the notifications store's unreadCount getter?

level: seniorimportance: should knowfreq 33%

basics

~20 s

Pass 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.

open as a page

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?

level: seniorimportance: should knowfreq 27%

basics

~20 s

createTestingPinia 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.

open as a page

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?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Install 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.

open as a page

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?

level: seniorimportance: nice to knowfreq 24%

basics

~20 s

A 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.

open as a page

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?

level: seniorimportance: nice to knowfreq 23%

basics

~20 s

acceptHMRUpdate 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.

open as a page

In a server-rendered Pinia setup store, a recently-viewed list read from localStorage is wiped on hydration; why, and what does skipHydrate() change?

level: seniorimportance: nice to knowfreq 23%

basics

~20 s

During 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.

open as a page

In a Pinia option store, what does store.$state = savedPrefs actually do to the existing state, and how does it differ from $patch(savedPrefs)?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

Assigning $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.

open as a page

A Pinia notifications store depends on a store plugin; how do you make that plugin apply in tests, and why not call testingPinia.use()?

level: seniorimportance: nice to knowfreq 21%

basics

~20 s

Plugins 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.

open as a page

In Options API components moving from Vuex to Pinia, how do mapStores, mapState, mapWritableState and mapActions replace Vuex's map helpers?

level: seniorimportance: nice to knowfreq 24%

basics

~10 s

Spread 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.

open as a page

showing 31–41 of 41