
4 min read
State Management in Vue: Composables, Provide/Inject, and Pinia
When to use a composable, provide/inject or Pinia in Vue, with TypeScript examples showing which components share the same state.
Ask my AI assistantMichael Hoffmann
@mokkapps

Two Vue components can call the same composable and still get separate state. That is useful for reusable logic, but it can be confusing when you expected both components to share a value.
In this article, I want to explain how I choose between composables, provide/inject and Pinia. Let's start with where the state is created and which components need access to it.
Reuse Logic With a Composable
A composable is a function that uses Vue's Composition API. We can use it to extract logic from a component and reuse that logic elsewhere.
Let's take a counter as an example:
import { ref } from 'vue'
export function useCounter(initial = 0) {
const count = ref(initial)
function increment() {
count.value++
}
function reset() {
count.value = initial
}
return { count, increment, reset }
}
The ref is created inside the function. Every call to useCounter() creates a new counter. If a header and a sidebar both call it, incrementing the header's counter won't change the sidebar's counter.
I use this approach when components need the same behavior with their own state, such as separate forms or visibility toggles. If only one component needs the logic and it is short, keeping it in that component is fine too.
Moving count outside the function would make the calls share it. Be careful with that approach in a server-rendered application: module-level state can be reused across requests. The Vue state management guide explains this SSR concern.
Share an Instance With Provide/Inject
Sometimes the state belongs to a group of components. A parent can create a counter and provide it to descendants, including components several levels below it.
First, define a typed injection key:
import type { InjectionKey } from 'vue'
import type { useCounter } from './composables/useCounter'
export const counterKey: InjectionKey<ReturnType<typeof useCounter>> = Symbol('counter')
The parent creates the counter instance and provides it:
<script setup lang="ts">
import { provide } from 'vue'
import CounterButton from './CounterButton.vue'
import { useCounter } from './composables/useCounter'
import { counterKey } from './counterContext'
const counter = useCounter()
provide(counterKey, counter)
</script>
<template>
<section>
<h2>Panel Counter</h2>
<CounterButton />
</section>
</template>
A descendant injects that same instance:
<script setup lang="ts">
import { inject } from 'vue'
import { counterKey } from './counterContext'
const counter = inject(counterKey)
if (!counter) {
throw new Error('CounterButton requires a CounterPanel parent')
}
const { count, increment } = counter
</script>
<template>
<button type="button" @click="increment">
Count: {{ count }}
</button>
</template>
Every CounterButton below that provider uses its counter. A second CounterPanel creates its own instance. The symbol avoids colliding with an unrelated provider's key, and InjectionKey gives inject() the expected TypeScript type.
I use provide/inject for state scoped to a component subtree, for example a form with nested inputs. For a direct parent-child relationship, props and events are usually easier to follow. Injection is helpful when otherwise unrelated intermediate components would only pass the same props through.
This example also shows how composables and provide/inject work together. The composable defines the behavior; the provider determines which components share its state.
Share Application State With Pinia
For state used across pages or unrelated parts of the application, I use Pinia. Its named stores give that state a place I can find again, and its devtools integration helps inspect changes.
Assuming Pinia is installed in your Vue app, we can define a preferences store like this:
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const usePreferencesStore = defineStore('preferences', () => {
const compactView = ref(false)
function toggleCompactView() {
compactView.value = !compactView.value
}
return { compactView, toggleCompactView }
})
A component can read that state and call its action:
<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { usePreferencesStore } from './stores/preferences'
const preferences = usePreferencesStore()
const { compactView } = storeToRefs(preferences)
</script>
<template>
<button type="button" @click="preferences.toggleCompactView">
Compact view: {{ compactView ? 'on' : 'off' }}
</button>
</template>
storeToRefs() lets us destructure reactive state without losing its connection to the store. Actions can be called directly on the store, as shown above. The Pinia documentation covers both setup stores and option stores.
Pinia doesn't automatically save preferences after a page reload. Persistence needs its own implementation or a plugin. For Nuxt, use the Pinia Nuxt module to handle its framework integration rather than creating a shared server-side singleton yourself.
Which One Would I Choose?
I start by asking which components should share the same state:
| Requirement | Approach |
|---|---|
| Reuse behavior with separate state for each caller | Composable |
| Share an instance within a component subtree | Provide/inject, often with a composable |
| Share named application state across pages | Pinia |
The number of components alone doesn't decide it. Ten independent forms can use a composable without sharing their values. Two distant components can justify a store if they need to read and change the same application state.
In Nuxt, useState is another option for shared state that is safe for server rendering. I explain it in When to Use useState in Nuxt.
Conclusion
I prefer to keep state close to the components that own it. A composable is enough when I want to reuse logic. I add provide/inject when descendants need the same instance, and use Pinia when state is shared across the application.
The mistake I try to avoid is introducing shared state when I only need shared code. Checking where the reactive value is created makes that distinction much easier to see.


