When you need a store

Intermediate · 6 min

Props, provide/inject, a shared composable, or Pinia — how to tell which one a piece of Vue state actually needs, and what a real store adds.

Before thisProvide and injectComposables

Make Apps with Danny · 6 min

When you need a store

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Make Apps with DannyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Most state in a Vue app should stay local to one component or be lifted to the nearest shared parent; a store is for state with no natural owner that distant parts of the tree read and write.
  • Data fetched from an API is server state, a cached copy of something the backend owns, and it is better held in a query cache than copied into a store by hand.
  • provide/inject shares state down one subtree without prop drilling, but it is scoped to wherever the provider sits and is awkward to reach from outside components.
  • A reactive() object at module scope, exported from a composable file, is already a working global store in a client-only app; Vue's reactivity system needs nothing more.
  • Pinia earns its place over a hand-rolled module store through devtools inspection and time travel, safe per-request state under SSR, hot module replacement, plugins, and strong TypeScript inference.

The problem starts with ownership

Every piece of state in a Vue app needs an owner: the component that holds it and decides how it changes. For most state the answer is obvious. A dropdown’s open/closed flag belongs to the dropdown. A form’s input values belong to the form. You declare a ref in <script setup>, and that is the end of it.

The trouble starts when the honest answer is “nobody in particular”. The logged-in user is shown in the navbar, checked by route guards, and edited on a settings page. A shopping cart is read by a header badge, a drawer, and a checkout route. None of those components is the obvious owner, and they don’t share a nearby parent.

The generic version of this argument — local, lifted, and global state — is made in the frontend roadmap’s client-side state management lesson. This lesson is about the Vue-specific ladder of tools, and where on it a store like Pinia actually starts to pay off.

Server state is a different problem

Before climbing the ladder, take one category of data off it entirely. A list of orders fetched from /api/orders is not really your state. The server owns it; your app holds a cached copy that can go stale the moment someone else changes it.

Putting that copy in a store is tempting, because a store is shared and the data is needed in several places. But a store doesn’t know when its copy is outdated, doesn’t refetch when the user comes back to the tab, and doesn’t dedupe two components asking for the same thing at once. You end up writing a worse cache by hand. React’s the case for a data-fetching library makes this argument at length, and it applies to Vue unchanged. In this group, data fetching in Vue and TanStack Query for Vue cover where server state should go instead.

What’s left for a store is client state: data the browser itself owns. The session, the cart before checkout, UI preferences, an in-progress multi-step form.

The ladder, rung by rung

Think of it like deciding where to keep a shared tool in an office. If one person uses it, it stays on their desk. If a team uses it, it goes on the team’s shelf. Only things the whole building needs go in the central supply room, because the supply room comes with sign-out sheets and someone to manage it.

Rung 1: local state. A ref in the component. This should be the default for everything.

Rung 2: lift it to a parent. Two sibling components need the same value, so it moves to their parent and flows down as props, with changes coming back up as emitted events or v-model.

Rung 3: provide/inject. When the shared parent is several layers up, passing the prop through every layer gets tedious. provide and inject let an ancestor hand state to any descendant directly:

<!-- TabsRoot.vue -->
<script setup>
import { provide, ref } from 'vue'

const activeTab = ref('overview')
provide('tabs', { activeTab, select: (id) => (activeTab.value = id) })
</script>

This is scoped state. Each TabsRoot on the page gets its own activeTab, which is exactly what you want for a reusable compound component. It is not a global store: the value exists only below the provider, and code outside components (a router guard, a plain utility module) cannot reach it.

Rung 4: a module-level composable. Here is the part of Vue that surprises people coming from other frameworks. Vue’s reactivity system is not tied to components. A reactive() object created at the top of a module is created once, when the module is first imported, and every component that imports it sees the same object:

// useCart.js
import { reactive, computed } from 'vue'

const state = reactive({ items: [] })

export function useCart() {
  const count = computed(() => state.items.reduce((n, i) => n + i.qty, 0))
  function add(product) {
    const line = state.items.find((i) => i.id === product.id)
    line ? line.qty++ : state.items.push({ ...product, qty: 1 })
  }
  return { items: state.items, count, add }
}

Any component can call useCart() and get the same cart. The Vue docs show exactly this pattern as “simple state management with the Reactivity API”. In a small client-only app, it is a perfectly reasonable store.

Rung 5: Pinia. The official store, covered from the next lesson on.

What Pinia adds over the module trick

If a reactive() object already works, why install anything? Because the module trick has real gaps once an app grows:

  • Server-side rendering. On a Node server, a module is evaluated once and shared by every request. A module-level reactive() object becomes shared between users, so one person’s cart can render into another person’s page. Pinia creates store instances per app, and an SSR server builds a fresh app per request, so state stays isolated.
  • Devtools. Pinia stores show up in Vue Devtools with their state, getters, and a timeline of actions and mutations you can inspect and step through. A hand-rolled object is just an anonymous reactive value.
  • Conventions. A store has a name, a defined shape, and a place in the stores/ folder. Ten developers writing module-level composables will write ten slightly different patterns.
  • Plugins and HMR. Persistence, logging, and undo can be added to every store from one place, and editing a store during development hot-reloads it without losing state.
  • TypeScript. Pinia infers the types of state, getters, and actions without hand-written interfaces.

None of that matters for a toggle. All of it matters for the session and cart of a real app.

The question to ask

Not “could another component use this?” Almost anything could. Ask instead:

  1. Is this server data? Then it belongs in a query cache, not a store.
  2. Does it belong to one component or one subtree? Then keep it local, lift it, or provide it.
  3. Do distant, unrelated parts of the app read and change it, possibly from outside components? Then it’s global client state, and Pinia is the right home.

Most apps end up with surprisingly few stores. That is a sign the earlier rungs are doing their job.

What the video covers

Make Apps with Danny’s short explainer starts from the problem complex, multi-page apps run into when data is scattered across components, and introduces the store as the fix: one central place, outside every component, holding the app’s core data in state, the methods that change it in actions, and derived values in getters. That is the same three-part shape Pinia uses. It treats the decision as “store or no store”. It doesn’t separate out server state, and it doesn’t show the module-level composable as a middle step. Those two pieces are what the sections above add, and they matter more as an app grows. The runner-up video from the same creator compares the composable approach directly with Vuex and Pinia; remember that Vuex is now in maintenance mode, as covered in Vuex and migrating to Pinia.

Where this fits

The next lesson, Pinia fundamentals, defines your first store and wires it into components.

Check yourself

  1. 01A header badge, a slide-out cart drawer and a checkout page, all in different routes, need the current cart contents. Which kind of state is this and where should it live?

    It is global client state: several distant components read and change it, it has no single owning component, and it is owned by the browser rather than fetched from a server. A store such as Pinia is the right home, because lifting it to a common parent would mean putting it at the app root and drilling it through every route.

  2. 02Why is a module-level reactive() object a problem in a server-rendered Vue app, when it works fine in a client-only one?

    On the server, a JavaScript module is evaluated once and shared by every incoming request, so a module-level reactive object becomes shared across users. One user's data can leak into another user's rendered page. Pinia avoids this by creating a fresh store instance per app instance, and the server creates one app instance per request.

  3. 03A list of products is fetched from an API and copied into a Pinia store so several pages can read it. What does the store fail to handle that a query cache would?

    The store has no idea when its copy has gone stale. It does not refetch when the user returns to the tab, dedupe parallel requests, expire unused data, or refresh after a mutation unless you write all of that yourself. Those are exactly the problems a query cache like TanStack Vue Query or Pinia Colada already solves.

  4. 04When is provide/inject a better choice than a store?

    When the state belongs to one subtree rather than the whole app, such as a form component sharing its validation state with deeply nested field components, or a tabs component sharing the active tab with its panels. Each instance of the provider gets its own state, which a single global store cannot give you.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark