Skip to content

Vue integration for LiveStore

The vue-livestore package provides integration with Vue. It’s currently in beta but aims to match feature parity with the React integration.

In order to use LiveStore with Vue, you need to wrap your application in a <LiveStoreProvider>.

<script setup lang="ts">
import { makeInMemoryAdapter } from '@livestore/adapter-web'
import { schema } from './schema.ts'
const adapter = makeInMemoryAdapter()
const storeId = 'demo-store'
const _options = { schema, adapter, storeId }
</script>
<template>
<LiveStoreProvider :options="options">
<template #loading>
<div>Loading LiveStore...</div>
</template>
<slot />
</LiveStoreProvider>
</template>
import {
import useStore
useStore
} from 'vue-livestore'
import {
import events
events
} from './schema.ts'
export const
const createTodo: () => void
createTodo
= () => {
const {
const store: any
store
} =
import useStore
useStore
()
const store: any
store
.
any
commit
(
import events
events
.
any
todoCreated
({
id: `${string}-${string}-${string}-${string}-${string}`
id
:
var crypto: Crypto
crypto
.
Crypto.randomUUID(): `${string}-${string}-${string}-${string}-${string}` (+1 overload)
randomUUID
(),
text: string
text
: 'Eat broccoli',
createdAt: Date
createdAt
: new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
() }))
}
<script setup lang="ts">
import { useQuery } from 'vue-livestore'
import { queryDb } from '@livestore/livestore'
import { tables } from './schema.ts'
const visibleTodos$ = queryDb(() => tables.todos.where({ completed: false }), { label: 'visibleTodos' })
const todos = useQuery(visibleTodos$)
void todos
</script>
<template>
<div>
<ul>
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
</li>
</ul>
</div>
</template>

[!] The interface for useClientDocument is experimental and might change

Since it’s more common in Vue to work with a single writable ref (as compared to state, setState in React) the useClientDocument composable for Vue tries to make that easier by directly returning a collection of refs.

The current implementation destructures all client state variables into the return object which allows directly binding to v-model or editing the .value reactivly.

<script setup lang="ts">
import { useClientDocument } from 'vue-livestore'
import { tables } from './schema.ts'
const { newTodoText, filter } = useClientDocument(tables.uiState)
void newTodoText
void filter
void newTodoText
void filter
</script>
<template>
<div>
<input type="text" v-model="newTodoText" />
<select v-model="filter">
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</div>
</template>

LiveStore and vue-livestore works with Vite out of the box.

Works out of the box with Nuxt if SSR is disabled by just wrapping the main content in a LiveStoreProvider. Example repo upcoming.

  • Vue-livestore uses the provider component pattern similar to the React integration. In Vue the plugin pattern is more common but it isn’t clear that that’s the most suitable structure for LiveStore in Vue. We might switch to the plugin pattern if we later find that more suitable especially with regards to Nuxt support and supporting multiple stores.