0
0
Vueframework~20 mins

Persisting store state in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vuex Persistence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Vuex persist state with plugins?

Consider a Vuex store using a plugin to save state to localStorage. What happens when the page reloads?

Vue
const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  },
  plugins: [store => {
    store.subscribe((mutation, state) => {
      localStorage.setItem('count', state.count)
    })
  }]
})
AThe count resets to 0 on reload because state is not restored automatically.
BThe count value doubles on each reload because the plugin runs twice.
CThe count value is saved but causes an error on reload due to missing initialization.
DThe count value is restored from localStorage automatically on reload.
Attempts:
2 left
💡 Hint

Think about what the plugin does and what it does not do on page load.

📝 Syntax
intermediate
2:00remaining
Which code correctly initializes Vuex state from localStorage?

You want to initialize the Vuex store state with a saved count from localStorage or default to 0. Which option is correct?

Astate: { count: parseInt(localStorage.getItem('count')) || 0 }
Bstate: { count: Number(localStorage.getItem('count')) || 0 }
Cstate: { count: localStorage.getItem('count') || 0 }
Dstate: { count: JSON.parse(localStorage.getItem('count')) || 0 }
Attempts:
2 left
💡 Hint

Remember localStorage stores strings. You need a number for count.

🔧 Debug
advanced
2:00remaining
Why does the persisted state not update after mutation?

Given this Vuex plugin code, why does localStorage not update after calling a mutation?

Vue
const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  },
  plugins: [store => {
    store.subscribe((mutation, state) => {
      localStorage.setItem('count', state.count)
    })
  }]
})

store.commit('increment')
// localStorage.getItem('count') still returns '0'
AThe plugin subscribes after the mutation, so it misses the update.
BThe mutation updates state.count, but localStorage stores the old value because subscribe callback runs before mutation.
CThe mutation changes state.count but localStorage.setItem is not called due to a typo.
DThe mutation is asynchronous, so localStorage updates before state changes.
Attempts:
2 left
💡 Hint

Consider when the subscribe callback runs relative to mutation.

state_output
advanced
2:00remaining
What is the value of state after reload with persisted state plugin?

Using this Vuex store with persisted state plugin, what is the value of state.count after a page reload?

Vue
const persistedState = store => {
  const saved = localStorage.getItem('count')
  if (saved) {
    store.replaceState({ count: Number(saved) })
  }
  store.subscribe((mutation, state) => {
    localStorage.setItem('count', state.count)
  })
}

const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  },
  plugins: [persistedState]
})

store.commit('increment')
// page reload happens here
ANaN
B0
Cundefined
D1
Attempts:
2 left
💡 Hint

Look at how replaceState uses the saved value on initialization.

🧠 Conceptual
expert
2:00remaining
Why use Vuex replaceState for persisting state?

Why is store.replaceState() preferred over directly mutating store.state when restoring persisted state?

AreplaceState automatically saves the state to localStorage, direct mutation does not.
BDirect mutation of store.state is forbidden and causes runtime errors, replaceState avoids errors.
CreplaceState triggers Vuex reactivity and updates all subscribers properly, while direct mutation does not.
DreplaceState is faster because it skips Vuex mutation handlers, while direct mutation is slow.
Attempts:
2 left
💡 Hint

Think about Vuex reactivity and how state changes notify components.