Skip to content
On this page

useRTDB

Category
Export Size
287 B
Package
@vueuse/firebase
Last Changed
7 months ago

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

Usage

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

const app = initializeApp({ /* config */ })
const db = getDatabase(app)

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

const app = initializeApp({ /* config */ })
const db = getDatabase(app)

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

You can reuse the db reference by passing autoDispose: false

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

or use createGlobalState from the core package

js
// 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')),
)
js
// app.js
import { useTodos } from './store'

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

const todos = useTodos()

Type Declarations

typescript
export interface UseRTDBOptions {
  autoDispose?: boolean
}
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @see https://vueuse.org/useRTDB
 */
export declare function useRTDB<T = any>(
  docRef: DatabaseReference,
  options?: UseRTDBOptions
): Ref<T | undefined>
export interface UseRTDBOptions {
  autoDispose?: boolean
}
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @see https://vueuse.org/useRTDB
 */
export declare function useRTDB<T = any>(
  docRef: DatabaseReference,
  options?: UseRTDBOptions
): Ref<T | undefined>

Source

SourceDocs

Contributors

Anthony Fu
Antério Vieira
Jelf
Robert Soriano
Phil Li

Changelog

v9.0.0-beta.2 on 7/24/2022
9c65f - feat(firebase)!: support firebase 9
v8.9.3 on 7/14/2022
606cd - fix!: rename type RTDBOptions to UseRTDBOptions (#1901)

Released under the MIT License.