Vue · Junior · Question 49 of 75

How is a Pinia store defined and used?

export const useCounterStore = defineStore("counter", () => {
  const count = ref(0);
  const double = computed(() => count.value * 2);
  function increment() { count.value++; }
  return { count, double, increment };
});
// in a component: const counter = useCounterStore();

Explanation

This is a setup store; there are also option stores with state, getters and actions. The store is created on first use and shared across components.

Further reading

Problem with a question

Vue · Junior · question 49

Test yourself

Take the Vue · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz