Question presented to candidate:
"A teammate wrote const { count, double } = useCounterStore() in a component, and the numbers on screen never change after the first render. Explain why, what storeToRefs does about it, and when you do not need it."
What a strong answer should cover:
- A Pinia store is a
reactive()object; destructuring reads each property once and copies the current value out, so the copies are no longer connected to the store. storeToRefs(store)returns an object of refs for every state property and getter, each linked to the store, so destructuring keeps reactivity; writing a state ref writes the store.- Actions are plain functions bound to the store, so
const { increment } = storeis fine andstoreToRefsdeliberately leaves them out. - Why not
toRefs(store): it also turns actions and internal$properties into refs (11 keys for a 3-member store in the verified run). - Alternatives: just use
store.count(no destructuring),toRef(store, 'count')for one value, orcomputed(() => store.count); getters stay read-only.
Clarifying questions expected:
- "Is this an option store or a setup store?" (the answer is the same, which is worth saying)
Code / implementation expected: Yes β the broken destructuring next to the storeToRefs version, and actions destructured directly.