Pinia is a state management library for Vue.js that helps you store and manage shared data across components in a simple and organized way.
Click to reveal answer
beginner
How do you create a basic Pinia store?
You create a Pinia store by defining a function with <code>defineStore</code> that returns state, getters, and actions. For example:<br><pre>import { defineStore } from 'pinia';
export const useMainStore = defineStore('main', {
state: () => ({ count: 0 }),
actions: { increment() { this.count++ } }
});</pre>
Click to reveal answer
beginner
What is the purpose of the state property in a Pinia store?
The state property holds the reactive data that the store manages. It is a function returning an object with all the data properties you want to share.
Click to reveal answer
beginner
How do you use a Pinia store inside a Vue component?
Inside a Vue component, you import the store and call it as a function to get access to its state and actions. For example:<br><pre>import { useMainStore } from '@/stores/main';
const mainStore = useMainStore();
mainStore.increment();</pre>
Click to reveal answer
beginner
Why should you use Pinia instead of local component state for shared data?
Pinia centralizes shared data so multiple components can access and update it easily. This avoids passing props deeply and keeps your app organized and easier to maintain.
Click to reveal answer
Which function is used to create a Pinia store?
AuseStore
BcreateStore
CdefineStore
DmakeStore
✗ Incorrect
The correct function to create a Pinia store is defineStore.
What does the state property in a Pinia store return?
AA plain object with methods
BA function returning an object with reactive data
CA string representing the store name
DAn array of actions
✗ Incorrect
The state property must be a function that returns an object containing reactive data.
How do you access a Pinia store inside a Vue component?
ACall the store function imported from the store file
BUse <code>this.$store</code> like Vuex
CUse <code>new Store()</code>
DImport and use <code>createStore()</code>
✗ Incorrect
You import the store and call it as a function to get the store instance.
Which of these is NOT a part of a Pinia store?
Astate
Bactions
Cgetters
Dcomponents
✗ Incorrect
Pinia stores have state, actions, and getters, but not components.
Why is Pinia preferred over local component state for shared data?
AIt centralizes data for easy sharing across components
BIt makes components heavier
CIt requires more code
DIt disables reactivity
✗ Incorrect
Pinia centralizes shared data, making it easier to manage and share across components.
Explain how to create and use a Pinia store in a Vue 3 application.
Think about the steps from defining the store to using it inside a component.
You got /4 concepts.
Describe the benefits of using Pinia for state management compared to local component state.
Consider why managing shared data in one place helps your app.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a Pinia store in a Vue application?
easy
A. To handle routing between pages
B. To style components with CSS
C. To hold and manage shared state across components
D. To write HTML templates
Solution
Step 1: Understand what Pinia stores do
Pinia stores are designed to hold shared data (state) that multiple components can access and update.
Step 2: Identify the correct purpose
Styling, routing, and HTML templates are handled by other parts of Vue or related libraries, not Pinia stores.
Final Answer:
To hold and manage shared state across components -> Option C
Quick Check:
Pinia stores = shared state management [OK]
Hint: Pinia stores manage shared data, not styles or routes [OK]
Common Mistakes:
Confusing Pinia with CSS or routing libraries
Thinking Pinia handles templates
Assuming Pinia is for styling
2. Which of the following is the correct way to define a Pinia store named 'user' using defineStore?
The store is correctly defined with state as a function returning an object, and actions modifying state properly.
Step 2: Identify missing parts
The code snippet does not show importing defineStore from 'pinia', which is required to use defineStore.
Final Answer:
Missing import of defineStore from 'pinia' -> Option A
Quick Check:
Always import defineStore before use [OK]
Hint: Always import defineStore from 'pinia' before defining stores [OK]
Common Mistakes:
Forgetting to import defineStore
Thinking state must be an object, not a function
Believing actions cannot modify state
5. You want to create a Pinia store that keeps track of a user's profile with a name and age, and includes a getter to check if the user is an adult (age >= 18). Which of the following is the correct way to define this store?