Skip to content
On this page

useRTDB

Category
Package
@vueuse/firebase
Last Changed
3 months ago

Reactive Firebase Realtime Database binding. Making it straightforward to always keep your local data in sync with remotes databases. available in add-on @vueuse/firebase

Usage

import firebase from 'firebase/app'
import 'firebase/database'
import { useRTDB } from '@vueuse/firebase/useRTDB'

const db = firebase
  .initializeApp({ databaseURL: 'https://MY-DATABASE.firebaseio.com' })
  .database()

// in setup()
const todos = useRTDB(db.ref('todos'))
import firebase from 'firebase/app'
import 'firebase/database'
import { useRTDB } from '@vueuse/firebase/useRTDB'

const db = firebase
  .initializeApp({ databaseURL: 'https://MY-DATABASE.firebaseio.com' })
  .database()

// in setup()
const todos = useRTDB(db.ref('todos'))

You can reuse the db reference by passing autoDispose: false

const todos = useRTDB(db.ref('todos'), { autoDispose: false })
const todos = useRTDB(db.ref('todos'), { autoDispose: false })

or use createGlobalStatefrom the core package

// store.js
import { createGlobalState } from '@vueuse/core'
import { useRTDB } from '@vueuse/firebase/useRTDB'

export const useTodos = createGlobalState(
  () => useRTDB(db.ref('todos')),
)
// store.js
import { createGlobalState } from '@vueuse/core'
import { useRTDB } from '@vueuse/firebase/useRTDB'

export const useTodos = createGlobalState(
  () => useRTDB(db.ref('todos')),
)
// app.js
import { useTodos } from './store'

const todos = useTodos()
// app.js
import { useTodos } from './store'

const todos = useTodos()

Type Declarations

export interface RTDBOptions {
  autoDispose?: boolean
}
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @param docRef
 * @param options
 */
export declare function useRTDB<T = any>(
  docRef: firebase.database.Reference,
  options?: RTDBOptions
): Ref<T | undefined>
export interface RTDBOptions {
  autoDispose?: boolean
}
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @param docRef
 * @param options
 */
export declare function useRTDB<T = any>(
  docRef: firebase.database.Reference,
  options?: RTDBOptions
): Ref<T | undefined>

Source

SourceDocs

Contributors

Anthony Fu
Antério Vieira
Robert Soriano
Phil Li

Changelog

v6.0.0-beta.2 on 8/9/2021
ff21b - feat: use tryOnScopeDispose instead of tryOnUnmounted
useRTDB has loaded