Skip to content

⬅️ Back to Table of Contents

📄 useRTDB

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 6
📐 Interfaces 1

📚 Table of Contents

🛠️ File Location:

📂 packages/firebase/useRTDB/index.ts

📦 Imports

Name Source
DatabaseReference firebase/database
DataSnapshot firebase/database
Ref vue
tryOnScopeDispose @vueuse/shared
onValue firebase/database
deepRef vue

Functions

useRTDB(docRef: DatabaseReference, options: UseRTDBOptions): Ref<T>

Reactive Firebase Realtime Database binding.

See: https://vueuse.org/useRTDB

Raw JSDoc
/**
 * Reactive Firebase Realtime Database binding.
 *
 * @see https://vueuse.org/useRTDB
 */

Calls:

  • console.error
  • deepRef (from vue)
  • snapshot.val
  • onValue (from firebase/database)
  • tryOnScopeDispose (from @vueuse/shared)
  • off
Code
export function useRTDB<T = any>(
  docRef: DatabaseReference,
  options: UseRTDBOptions = {},
) {
  const {
    errorHandler = (err: Error) => console.error(err),
    autoDispose = true,
  } = options
  const data = deepRef(undefined) as Ref<T | undefined>

  function update(snapshot: DataSnapshot) {
    data.value = snapshot.val()
  }

  const off = onValue(docRef, update, errorHandler)

  if (autoDispose)
    tryOnScopeDispose(() => off())

  return data
}

Internal helpers

Declared inside another function in this file.

update(snapshot: DataSnapshot): void

Parameters:

  • snapshot DataSnapshot

Returns: void

Calls:

  • snapshot.val
Code
function update(snapshot: DataSnapshot) {
    data.value = snapshot.val()
  }

Interfaces

UseRTDBOptions

Interface Code
export interface UseRTDBOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean
}

Properties

Name Type Optional Description
errorHandler (err: Error) => void not shown
autoDispose boolean not shown

Generated by Syntax Scribe