State in stores holds shared data that many parts of your app can use. It helps keep your app organized and consistent.
Defining state in stores in Vue
import { defineStore } from 'pinia' export const useStore = defineStore('storeId', { state: () => ({ count: 0, name: '' }) })
The state is a function returning an object with your data.
The store ID (first argument) should be unique and descriptive.
count number starting at 0.import { defineStore } from 'pinia' export const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }) })
import { defineStore } from 'pinia' export const useUserStore = defineStore('user', { state: () => ({ name: '', loggedIn: false }) })
This example creates a store with a count state. The app shows the count and a button to increase it. The count is shared through the store.
import { createApp } from 'vue' import { createPinia, defineStore } from 'pinia' const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }) }) const App = { template: ` <div> <p>Count: {{ counter.count }}</p> <button @click="counter.count++">Increase</button> </div> `, setup() { const counter = useCounterStore() return { counter } } } const app = createApp(App) app.use(createPinia()) app.mount('#app')
Always define state as a function returning an object to avoid shared state between store instances.
Use descriptive store IDs to avoid confusion in bigger apps.
Pinia stores are reactive, so UI updates automatically when state changes.
Stores hold shared state for your Vue app.
Define state as a function returning an object inside defineStore.
Use stores to keep data consistent and easy to manage across components.
Practice
defineStore?Solution
Step 1: Understand Vue store state definition
Vue stores require state to be a function returning an object to avoid shared state across instances.Step 2: Compare options with Vue pattern
Only defining state as a function returning an object matches Vue's recommended pattern.Final Answer:
Define state as a function that returns an object. -> Option CQuick Check:
State = function returning object [OK]
- Defining state as a plain object causes shared state bugs
- Using arrays or strings instead of objects for state
- Forgetting to return the object from the state function
Solution
Step 1: Recall defineStore syntax
The first argument is the store id string, the second is an object with a state function returning the state object.Step 2: Check each option
const useUserStore = defineStore('user', { state: () => ({ name: '' }) }) correctly uses defineStore with id 'user' and state as a function returning { name: '' }.Final Answer:
const useUserStore = defineStore('user', { state: () => ({ name: '' }) }) -> Option DQuick Check:
defineStore(id, { state: () => ({}) }) [OK]
- Defining state as an object instead of a function
- Passing state function as the second argument directly
- Using an object with 'id' property instead of string id
store.count after calling const store = useCounterStore()?
const useCounterStore = defineStore('counter', {
state: () => ({ count: 10 })
})Solution
Step 1: Analyze the state function
The state function returns an object with count initialized to 10.Step 2: Understand store instance value
When useCounterStore() is called, it creates a store instance with count set to 10 as initial state.Final Answer:
10 -> Option AQuick Check:
Initial count = 10 [OK]
- Assuming count defaults to 0 if not explicitly set
- Confusing state function with plain object
- Expecting undefined because of missing initialization
const useTodoStore = defineStore('todo', {
state: {
todos: []
}
})Solution
Step 1: Check state definition format
State must be a function returning an object to avoid shared state across instances.Step 2: Identify the error
The code defines state as an object directly, which is incorrect.Final Answer:
State must be a function returning an object, not an object directly. -> Option BQuick Check:
State = function returning object, not plain object [OK]
- Defining state as a plain object causes shared state bugs
- Misunderstanding store id type requirements
- Incorrect usage of defineStore arguments
defineStore?Solution
Step 1: Define state as a function returning an object
The state must be a function returning an object with users array.Step 2: Define actions inside the actions property
Actions are methods inside the actions object to modify state safely.Step 3: Verify correct syntax
defineStore('users', { state: () => ({ users: [] }), actions: { addUser(user) { this.users.push(user) } } }) correctly defines state as a function and actions as an object with addUser method.Final Answer:
defineStore('users', { state: () => ({ users: [] }), actions: { addUser(user) { this.users.push(user) } } }) -> Option AQuick Check:
State function + actions object = correct store [OK]
- Defining state as an object instead of a function
- Placing methods outside the actions property
- Using 'methods' instead of 'actions' in defineStore
Ready to prove it?
Pass the test and this pattern moves unknown
