Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useRTDB.md
1---2category: '@Firebase'3---45# useRTDB67Reactive [Firebase Realtime Database](https://firebase.google.com/docs/database) binding. Making it straightforward to **always keep your local data in sync** with remotes databases.89## Usage1011```ts12import { useRTDB } from '@vueuse/firebase/useRTDB'13import { initializeApp } from 'firebase/app'14import { getDatabase } from 'firebase/database'1516const app = initializeApp({ /* config */ })17const db = getDatabase(app)1819// in setup()20const todos = useRTDB(db.ref('todos'))21```2223## Options2425| Option | Type | Default | Description |26| -------------- | ---------------------- | --------------- | --------------------------------------------------------- |27| `autoDispose` | `boolean` | `true` | Automatically unsubscribe when the component is unmounted |28| `errorHandler` | `(err: Error) => void` | `console.error` | Custom error handler for database errors |2930## Return Value3132Returns a `Ref<T | undefined>` that is automatically updated when the database value changes.3334## Reusing Database References3536You can reuse the db reference by passing `autoDispose: false`3738```ts39const todos = useRTDB(db.ref('todos'), { autoDispose: false })40```4142or use `createGlobalState` from the core package4344```ts twoslash include store45// @filename: store.ts46// ---cut---47// store.ts48import { createGlobalState } from '@vueuse/core'49import { useRTDB } from '@vueuse/firebase/useRTDB'5051export const useTodos = createGlobalState(52() => useRTDB(db.ref('todos')),53)54```5556```vue57<!-- app.vue -->58<script setup lang="ts">59// @include: store60// ---cut---61import { useTodos } from './store'6263const todos = useTodos()64</script>65```6667## Type Declarations6869```ts70export interface UseRTDBOptions {71errorHandler?: (err: Error) => void72autoDispose?: boolean73}74/**75* Reactive Firebase Realtime Database binding.76*77* @see https://vueuse.org/useRTDB78*/79export declare function useRTDB<T = any>(80docRef: DatabaseReference,81options?: UseRTDBOptions,82): Ref<T | undefined, T | undefined>83```84