Skip to main content
TanStack Store provides Vue support through the @tanstack/vue-store package, which integrates with Vue’s reactivity system using composables. It supports both Vue 2.7+ and Vue 3.

Installation

Install the Vue adapter package:
The @tanstack/vue-store package re-exports everything from @tanstack/store, so you only need to install the Vue package.

Vue Version Compatibility

The package uses vue-demi to support multiple Vue versions:
  • Vue 3.x (recommended)
  • Vue 2.7+
  • Vue 2.6 with @vue/composition-api

Basic Usage

The primary way to use TanStack Store in Vue is through the useStore composable:

The useStore Composable

The useStore composable subscribes your component to store updates and returns a readonly ref that automatically updates.

Signature

Parameters

  • store - The store instance to subscribe to
  • selector - (Optional) A function that selects which part of the state you need. Defaults to returning the entire state
  • options.equal - (Optional) A custom equality function. Defaults to shallow

Return Value

Returns a readonly Vue Ref containing the selected state. Use .value to access the state value in script, or directly in templates.

Selector Optimization

The selector function allows you to subscribe to only the parts of state you need:

Custom Equality Functions

For complex state selections, provide a custom equality function:

Shallow Equality

The package exports a shallow utility for shallow object comparison (this is the default):

Complete Example: Todo App

Here’s a complete Todo application using Vue 3 Composition API:

Options API Support

While the Composition API is recommended, you can use stores with the Options API:

Vue-Specific Considerations

Reactivity System Integration

The Vue adapter integrates deeply with Vue’s reactivity system:
  • Uses watch to subscribe to store changes
  • Returns readonly refs to prevent accidental mutations
  • Automatically cleans up subscriptions when components unmount

Server-Side Rendering (SSR)

TanStack Store works with Vue SSR. Create stores at module level and initialize them with server data:

Nuxt Integration

For Nuxt 3, create a plugin to initialize stores:

Performance Tips

  1. Use selective subscriptions: Subscribe only to the state you need
  2. Leverage the default shallow equality: It works well for most cases
  3. Create focused stores: Multiple small stores perform better than one large store
  4. Use computed refs: For derived state within components, use Vue’s computed

Derived Stores

Create derived stores that react to other stores:

API Reference

For detailed API documentation, see:
    • useStore - Vue composable for subscribing to stores
    • shallow - Shallow equality comparison utility
  • createStore - Core store creation API

TypeScript Support

The Vue adapter provides full TypeScript support: