Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import Pinia's store creation function.
Vue
import { [1] } from 'pinia';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'createStore' which is from Vuex, not Pinia.
Using 'useStore' which is a hook, not the store creator.
✗ Incorrect
The defineStore function is used to create a Pinia store.
2fill in blank
mediumComplete the code to define a Pinia store named 'counter'.
Vue
export const useCounterStore = [1]('counter', { state: () => ({ count: 0 }) });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'createStore' which is not from Pinia.
Forgetting to name the store.
✗ Incorrect
defineStore is used to create a named store in Pinia.
3fill in blank
hardFix the error in the state typing to make the store type-safe.
Vue
export const useUserStore = defineStore('user', { state: (): [1] => ({ name: '', age: 0 }) });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' which disables type checking.
Using 'object' which is too generic.
✗ Incorrect
Using a specific interface like UserState ensures type safety for the state.
4fill in blank
hardFill both blanks to add a typed getter and an action in the Pinia store.
Vue
export const useTodoStore = defineStore('todo', { state: () => ({ todos: [] as string[] }), getters: { todoCount: (state) => state.[1].length }, actions: { addTodo(todo: string) { this.[2].push(todo); } } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the same state property.
Using undefined properties in getters or actions.
✗ Incorrect
The state property is named todos, so both getter and action must use it.
5fill in blank
hardFill all three blanks to create a fully typed Pinia store with state, getter, and action.
Vue
interface ProductState {
products: string[];
}
export const useProductStore = defineStore('product', {
state: (): [1] => ({ products: [] }),
getters: {
productCount: (state) => state.[2].length
},
actions: {
addProduct(product: string) {
this.[3].push(product);
}
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong interface name for state typing.
Mismatching property names in getter or action.
✗ Incorrect
The state is typed as ProductState, and the property is products used in getter and action.