← Back to Vue.js Course | Chapter 8: State Management | Lesson 1 of 6

What is Pinia

Pinia is Vue's official store: a central place to keep state that many components share.

In this page:

  1. What is Pinia

What is Pinia

When several components need the same data, passing props through many layers gets painful. A Pinia store holds that state in one place, and components read and update it directly.

Pinia replaced Vuex as the recommended library, with a simpler API, TypeScript support and DevTools integration.

Note: Pinia is the official successor to Vuex for Vue 3.

Example: What is Pinia

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script src="https://unpkg.com/[email protected]/lib/index.iife.js"></script>
  <script src="https://unpkg.com/pinia@2/dist/pinia.iife.js"></script>
</head>
<body>
<div id="app">
  <p>Header sees: {{ counter.count }}</p>
  <p>Footer sees: {{ counter.count }}</p>
  <button @click="counter.count++">Increment shared count</button>
</div>
<script>
  const useCounter = Pinia.defineStore("counter", { state: () => ({ count: 0 }) });
  const app = Vue.createApp({ setup() { return { counter: useCounter() }; } });
  app.use(Pinia.createPinia());
  app.mount("#app");
  document.querySelector("button").click();
  Vue.nextTick(() => console.log(document.querySelector("#app").textContent.replace(/\s+/g, " ").trim()));
</script>
</body>
</html>

<!-- Output:
Rendered: Header sees: 1Footer sees: 1Increment shared count
console: Header sees: 1Footer sees: 1Increment shared count
-->
Live Example
Related Topics
Common Mistakes
  1. Putting all state in a store unnecessarily
  2. Using Vuex for new Vue 3 projects
  3. Mutating state outside the store without care
Chapter Summary
  • A store is shared reactive state
  • Pinia is the official store
  • It replaced Vuex
  • Great for cross-component data
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.