Consider a Vue 3 store with this action to increment a counter asynchronously. What will be the value of state.count after dispatching incrementAsync once?
import { reactive } from 'vue' const state = reactive({ count: 0 }) const actions = { incrementAsync() { setTimeout(() => { state.count++ }, 1000) } } actions.incrementAsync() // After 1 second, what is state.count?
Think about when setTimeout runs and when the state changes.
The action uses setTimeout to delay incrementing state.count by 1 after 1 second. So initially, state.count is 0, then after 1 second it becomes 1.
In Vue 3 with Composition API, which code snippet correctly defines an action that commits a mutation to update state?
Remember that actions receive a context object to commit mutations.
Actions receive a context object which has the commit method to call mutations. Option B correctly uses context.commit. Option B misses context, A wrongly uses dispatch, and D tries to mutate state directly in an action.
Given this Vue 3 store code, why does calling actions.increment() not cause a UI update for state.count?
import { reactive } from 'vue' const state = reactive({ count: 0 }) const actions = { increment() { state.count += 1 } } // Called somewhere: actions.increment()
Think about how Vue reactivity works and what triggers UI updates.
The state is reactive but if it is not used or referenced in any component or template, Vue will not track or update the UI. The action does update the state.count value, but if the state is not connected to a component, no visible change occurs.
Given this Vue 3 store code, what is the value of state.items after calling actions.addItem('apple') then actions.removeItem('apple')?
import { reactive } from 'vue' const state = reactive({ items: [] }) const actions = { addItem(item) { state.items.push(item) }, removeItem(item) { const index = state.items.indexOf(item) if (index !== -1) { state.items.splice(index, 1) } } } actions.addItem('apple') actions.removeItem('apple')
Think about how push and splice affect the array.
The first action adds 'apple' to the items array. The second action finds 'apple' and removes it. So the final array is empty.
In Vue 3, why is it recommended to use actions to modify state instead of changing state directly in components?
Think about maintainability and debugging in larger apps.
Using actions centralizes all state changes in one place, making it easier to track, debug, and maintain the app. Direct mutation is allowed but discouraged for clarity and control. Vue reactivity still works with direct mutation, and actions do not automatically batch updates.