0
0
Vueframework~20 mins

Creating a Pinia store in Vue - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pinia Store Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the initial state of this Pinia store?
Given the Pinia store below, what is the value of count when the store is first used in a component?
Vue
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 10
  }),
  actions: {
    increment() {
      this.count++
    }
  }
})
Aundefined
BNaN
C0
D10
Attempts:
2 left
💡 Hint
Look at the state function's returned object.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Pinia store with a getter?
Select the option that correctly defines a Pinia store with a getter named doubleCount that returns count * 2.
A
export const useStore = defineStore('main', {
  state: () => ({ count: 1 }),
  getters: {
    doubleCount: (state) => state.count * 2
  }
})
B
export const useStore = defineStore('main', {
  state: () => ({ count: 1 }),
  getters: {
    doubleCount() {
      return this.count * 2
    }
  }
})
C
export const useStore = defineStore('main', {
  state: () => ({ count: 1 }),
  getters: {
    doubleCount: () => this.count * 2
  }
})
D
export const useStore = defineStore('main', {
  state: () => ({ count: 1 }),
  getters: {
    doubleCount: function(state) {
      return state.count * 2
    }
  }
})
Attempts:
2 left
💡 Hint
Pinia getters receive state as the first argument and should not use this.
🔧 Debug
advanced
2:00remaining
Why does this Pinia store action not update the state?
Consider this Pinia store code. Why does calling increment() not change count?
Vue
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      let count = this.count
      count++
    }
  }
})
AThe store is missing a mutation to update the state.
BThe action increments a local variable instead of the store state property.
CThe state is immutable and cannot be changed inside actions.
DThe <code>this</code> keyword is not bound correctly in the action.
Attempts:
2 left
💡 Hint
Look at what variable is being incremented inside the action.
state_output
advanced
2:00remaining
What is the value of fullName after calling setName('Alice', 'Smith')?
Given this Pinia store, what is the value of fullName after calling the action setName('Alice', 'Smith')?
Vue
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    firstName: '',
    lastName: ''
  }),
  getters: {
    fullName: (state) => `${state.firstName} ${state.lastName}`
  },
  actions: {
    setName(first, last) {
      this.firstName = first
      this.lastName = last
    }
  }
})

// Usage:
const userStore = useUserStore()
userStore.setName('Alice', 'Smith')
const result = userStore.fullName
A" "
B"Alice"
C"Alice Smith"
D"Smith"
Attempts:
2 left
💡 Hint
Check how the getter combines firstName and lastName.
🧠 Conceptual
expert
2:00remaining
Why use defineStore with an ID string in Pinia?
What is the main reason Pinia requires an ID string when creating a store with defineStore?
ATo uniquely identify the store for devtools and state persistence.
BTo set the initial state of the store automatically.
CTo bind the store to a specific Vue component instance.
DTo enable TypeScript type inference for the store.
Attempts:
2 left
💡 Hint
Think about how Pinia tracks and manages multiple stores.