Given the following Vue 3 component using Pinia store, what will be rendered inside the <div>?
import { defineComponent } from 'vue'; import { useCounterStore } from '@/stores/counter'; export default defineComponent({ setup() { const counter = useCounterStore(); return { counter }; }, template: `<div>{{ counter.count }}</div>` }); // Pinia store definition: // import { defineStore } from 'pinia'; // export const useCounterStore = defineStore('counter', { // state: () => ({ count: 5 }), // actions: { // increment() { this.count++; } // } // });
Look at the initial state of the store's count property.
The Pinia store useCounterStore initializes count to 5. The component accesses counter.count and displays it, so the output is 5.
message after clicking the button?Consider this Vue 3 component using a Pinia store. What will be the value of message after the button is clicked once?
import { defineComponent, ref } from 'vue'; import { useMessageStore } from '@/stores/message'; export default defineComponent({ setup() { const store = useMessageStore(); const message = ref(store.text); function update() { store.updateText('Hello Vue!'); message.value = store.text; } return { message, update }; }, template: `<div><p>{{ message }}</p><button @click="update">Update</button></div>` }); // Pinia store definition: // import { defineStore } from 'pinia'; // export const useMessageStore = defineStore('message', { // state: () => ({ text: 'Initial' }), // actions: { // updateText(newText) { this.text = newText; } // } // });
Think about how message is assigned and updated.
The message ref is initialized with the store's text value but does not reactively track store changes. Updating the store's text does not update message automatically. So after clicking, message remains 'Initial'.
Identify the correct way to import and use a Pinia store named useTodoStore inside a Vue 3 component's setup function.
Remember how to import named exports and call functions inside setup.
Option C correctly imports the named export useTodoStore and calls it inside setup. Option C imports as default which is incorrect. Option C assigns the function itself without calling it. Option C does not return the store, so it won't be accessible in the template.
Examine the code below. The component tries to update the Pinia store's count when the button is clicked, but the displayed count does not change. What is the cause?
import { defineComponent } from 'vue'; import { useCounterStore } from '@/stores/counter'; export default defineComponent({ setup() { const counter = useCounterStore(); function increment() { counter.count += 1; } return { counter, increment }; }, template: `<div><p>{{ counter.count }}</p><button @click="increment">Add</button></div>` }); // Pinia store: // export const useCounterStore = defineStore('counter', { // state: () => ({ count: 0 }), // actions: { // increment() { this.count++; } // } // });
Consider how Pinia tracks changes and the recommended way to update state.
Pinia recommends updating state via actions to ensure reactivity and proper tracking. Directly modifying counter.count may not trigger updates in some setups. Using the store's increment action ensures reactive updates.
Choose the best explanation for why developers use Pinia stores in Vue 3 applications.
Think about what state management means in an app.
Pinia is a state management library that centralizes and shares reactive state across Vue components. It does not generate UI, handle routing, or compile templates.