What is wrong if dispatching updateName does not update the state?
medium
A. The action should use dispatch instead of commit
B. The mutation setName is missing from the store
C. The action is missing the context parameter
D. The action is not properly registered or dispatched
Solution
Step 1: Check action and mutation code
The action correctly commits the mutation with the new name, and the mutation updates the state.
Step 2: Consider why state doesn't update
If dispatching the action doesn't update state, likely the action is not properly registered in the store or the dispatch call is incorrect.
Final Answer:
The action is not properly registered or dispatched -> Option D
Quick Check:
Unregistered or wrong dispatch causes no state update = A [OK]
Hint: Check action registration and dispatch call if state doesn't update [OK]
Common Mistakes:
Using commit inside action is correct, not an error
Assuming mutation is missing when it exists
Confusing commit and dispatch roles
5. You want to create a Vuex action that fetches user data from an API and updates the state only if the user is active. Which of the following action implementations correctly applies this logic?
hard
A. async fetchUser({ commit }) {
const user = await fetch('/api/user').then(res => res.json())
commit('setUser', user)
}
C. async fetchUser({ commit }) {
const user = await fetch('/api/user').then(res => res.json())
if (user.active) commit('setUser', user)
}
D. fetchUser({ commit }) {
const user = fetch('/api/user').then(res => res.json())
if (user.active) commit('setUser', user)
}
Solution
Step 1: Analyze async/await usage
async fetchUser({ commit }) {
const user = await fetch('/api/user').then(res => res.json())
if (user.active) commit('setUser', user)
} correctly awaits the fetch call and then checks if the user is active before committing the mutation.
Step 2: Identify issues in other options
fetchUser({ commit }) {
fetch('/api/user').then(res => res.json()).then(user => {
commit('setUser', user)
})
} commits without checking active status. async fetchUser({ commit }) {
const user = await fetch('/api/user').then(res => res.json())
commit('setUser', user)
} commits without condition. fetchUser({ commit }) {
const user = fetch('/api/user').then(res => res.json())
if (user.active) commit('setUser', user)
} does not await fetch, so user is a Promise, causing errors.
Final Answer:
async fetchUser({ commit }) {
const user = await fetch('/api/user').then(res => res.json())
if (user.active) commit('setUser', user)
} -> Option C
Quick Check:
Await fetch and check condition before commit = C [OK]
Hint: Await fetch, check condition, then commit mutation [OK]
Common Mistakes:
Not awaiting fetch causing Promise errors
Committing without condition check
Using then without proper chaining or condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.