0
0
Vueframework~20 mins

Using stores in components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pinia Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue component display?

Given the following Vue 3 component using Pinia store, what will be rendered inside the <div>?

Vue
import { defineComponent } from 'vue';
import { useCounterStore } from '@/stores/counter';

export default defineComponent({
  setup() {
    const counter = useCounterStore();
    return { counter };
  },
  template: `<div>{{ counter.count }}</div>`
});

// Pinia store definition:
// import { defineStore } from 'pinia';
// export const useCounterStore = defineStore('counter', {
//   state: () => ({ count: 5 }),
//   actions: {
//     increment() { this.count++; }
//   }
// });
A5
B0
Cundefined
DError: counter is not defined
Attempts:
2 left
💡 Hint

Look at the initial state of the store's count property.

state_output
intermediate
2:00remaining
What is the value of message after clicking the button?

Consider this Vue 3 component using a Pinia store. What will be the value of message after the button is clicked once?

Vue
import { defineComponent, ref } from 'vue';
import { useMessageStore } from '@/stores/message';

export default defineComponent({
  setup() {
    const store = useMessageStore();
    const message = ref(store.text);
    function update() {
      store.updateText('Hello Vue!');
      message.value = store.text;
    }
    return { message, update };
  },
  template: `<div><p>{{ message }}</p><button @click="update">Update</button></div>`
});

// Pinia store definition:
// import { defineStore } from 'pinia';
// export const useMessageStore = defineStore('message', {
//   state: () => ({ text: 'Initial' }),
//   actions: {
//     updateText(newText) { this.text = newText; }
//   }
// });
AError: message is not reactive
BHello Vue!
Cundefined
DInitial
Attempts:
2 left
💡 Hint

Think about how message is assigned and updated.

📝 Syntax
advanced
2:00remaining
Which option correctly imports and uses a Pinia store in a Vue 3 component?

Identify the correct way to import and use a Pinia store named useTodoStore inside a Vue 3 component's setup function.

A
import { useTodoStore } from '@/stores/todo';

export default {
  setup() {
    const todoStore = useTodoStore;
    return { todoStore };
  }
};
B
import useTodoStore from '@/stores/todo';

export default {
  setup() {
    const todoStore = useTodoStore();
    return { todoStore };
  }
};
C
import { useTodoStore } from '@/stores/todo';

export default {
  setup() {
    const todoStore = useTodoStore();
    return { todoStore };
  }
};
D
import { useTodoStore } from '@/stores/todo';

export default {
  setup() {
    const todoStore = useTodoStore();
  }
};
Attempts:
2 left
💡 Hint

Remember how to import named exports and call functions inside setup.

🔧 Debug
advanced
2:00remaining
Why does this Vue component fail to update the store state reactively?

Examine the code below. The component tries to update the Pinia store's count when the button is clicked, but the displayed count does not change. What is the cause?

Vue
import { defineComponent } from 'vue';
import { useCounterStore } from '@/stores/counter';

export default defineComponent({
  setup() {
    const counter = useCounterStore();
    function increment() {
      counter.count += 1;
    }
    return { counter, increment };
  },
  template: `<div><p>{{ counter.count }}</p><button @click="increment">Add</button></div>`
});

// Pinia store:
// export const useCounterStore = defineStore('counter', {
//   state: () => ({ count: 0 }),
//   actions: {
//     increment() { this.count++; }
//   }
// });
ADirectly modifying <code>counter.count</code> bypasses Pinia's reactivity; must use store action.
BThe store is not properly imported, causing reactivity to fail.
CThe template syntax is incorrect; must use <code>v-text</code> instead of interpolation.
DThe <code>increment</code> function is not returned from <code>setup</code>.
Attempts:
2 left
💡 Hint

Consider how Pinia tracks changes and the recommended way to update state.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using Pinia stores in Vue components?

Choose the best explanation for why developers use Pinia stores in Vue 3 applications.

APinia replaces Vue Router to handle navigation between pages.
BPinia centralizes state management, making state reactive and shared across components easily.
CPinia automatically generates UI components based on state definitions.
DPinia compiles Vue templates into optimized JavaScript functions.
Attempts:
2 left
💡 Hint

Think about what state management means in an app.