0
0
Vueframework~20 mins

Defining state in stores in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Store State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does the state update in a Vue store?

Consider a Vue store defined with reactive state. What happens when you update a property inside the state object?

Vue
import { reactive } from 'vue';

const store = {
  state: reactive({ count: 0 })
};

function increment() {
  store.state.count++;
}

increment();
console.log(store.state.count);
AThe state count throws an error because reactive cannot be used in stores.
BThe state count updates to 1 and any component using it re-renders.
CThe state count updates to 1 but components do not re-render automatically.
DThe state count remains 0 because reactive objects are immutable.
Attempts:
2 left
💡 Hint

Think about how Vue's reactive function tracks changes.

📝 Syntax
intermediate
1:30remaining
Identify the correct way to define state in a Vue store

Which of the following correctly defines a reactive state object inside a Vue store?

Aconst state = reactive user: null };
Bconst state = ref({ user: null });
Cconst state = reactive([{ user: null }]);
Dconst state = reactive({ user: null });
Attempts:
2 left
💡 Hint

Remember the syntax for reactive requires an object inside parentheses.

🔧 Debug
advanced
2:30remaining
Why does this Vue store state not update the component?

Given this store code, why does the component not update when state.count changes?

Vue
import { reactive } from 'vue';

const state = { count: 0 };

function increment() {
  state.count++;
}

export default { state, increment };
ABecause the increment function is not called inside a Vue lifecycle hook.
BBecause <code>count</code> is a number and numbers cannot be reactive.
CBecause <code>state</code> is not reactive; it should be wrapped with <code>reactive()</code>.
DBecause the store must use <code>ref</code> instead of <code>reactive</code>.
Attempts:
2 left
💡 Hint

Check how the state object is created and if Vue can track its changes.

state_output
advanced
2:00remaining
What is the value of state after these mutations?

Given this Vue store code, what is the final value of state.items?

Vue
import { reactive } from 'vue';

const state = reactive({ items: [] });

function addItem(item) {
  state.items.push(item);
}

addItem('apple');
addItem('banana');
state.items = ['orange'];
A['orange']
B['apple', 'banana']
C['apple', 'banana', 'orange']
D[]
Attempts:
2 left
💡 Hint

Consider what happens when you assign a new array to a reactive property.

🧠 Conceptual
expert
3:00remaining
Why use a function to define state in Vue stores?

In Vue 3 stores, why is it recommended to define the state as a function returning an object instead of a plain object?

ATo ensure each store instance has its own independent reactive state, avoiding shared state bugs.
BBecause Vue requires all state to be functions for reactivity to work.
CTo improve performance by caching the state object across components.
DBecause functions automatically make the state deeply reactive.
Attempts:
2 left
💡 Hint

Think about what happens if multiple components use the same store instance.