0
0
Vueframework~20 mins

Actions for modifying state in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Vue action is dispatched?

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?

Vue
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?
A0 immediately, then 1 after 1 second
B1 immediately
C0 forever, no change
DThrows an error because state is reactive
Attempts:
2 left
💡 Hint

Think about when setTimeout runs and when the state changes.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Vue action that commits a mutation?

In Vue 3 with Composition API, which code snippet correctly defines an action that commits a mutation to update state?

Aconst actions = { updateName(name) { commit('setName', name) } }
Bconst actions = { updateName(context, name) { context.commit('setName', name) } }
Cconst actions = { updateName(context, name) { context.dispatch('setName', name) } }
Dconst actions = { updateName(state, name) { state.name = name } }
Attempts:
2 left
💡 Hint

Remember that actions receive a context object to commit mutations.

🔧 Debug
advanced
2:00remaining
Why does this Vue action not cause a UI update?

Given this Vue 3 store code, why does calling actions.increment() not cause a UI update for state.count?

Vue
import { reactive } from 'vue'

const state = reactive({ count: 0 })

const actions = {
  increment() {
    state.count += 1
  }
}

// Called somewhere: actions.increment()
ABecause the state object is not exported or used in a component
BBecause actions must use a commit function to change state
CBecause state is reactive, direct mutation inside actions is ignored
DBecause the increment function is not bound to the store context
Attempts:
2 left
💡 Hint

Think about how Vue reactivity works and what triggers UI updates.

state_output
advanced
2:00remaining
What is the final state after dispatching these Vue actions?

Given this Vue 3 store code, what is the value of state.items after calling actions.addItem('apple') then actions.removeItem('apple')?

Vue
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')
A['apple', 'apple']
B['apple']
Cundefined
D[] (empty array)
Attempts:
2 left
💡 Hint

Think about how push and splice affect the array.

🧠 Conceptual
expert
2:00remaining
Why use actions instead of directly mutating state in Vue 3?

In Vue 3, why is it recommended to use actions to modify state instead of changing state directly in components?

ADirect mutation breaks Vue reactivity and stops UI updates
BDirect state mutation is forbidden and causes runtime errors
CActions centralize state changes, making them easier to track and debug
DActions automatically optimize performance by batching updates
Attempts:
2 left
💡 Hint

Think about maintainability and debugging in larger apps.