Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 6
📊 Variables & Constants 1
📐 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

Variables & Constants

Name Type Kind Value Exported
data Ref<T> const deepRef(undefined) as Ref<T | undefined>

Functions

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

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
}
  • JSDoc:

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

  • Parameters:

  • docRef: DatabaseReference
  • options: UseRTDBOptions
  • Return Type: Ref<T>
  • Calls:
  • console.error
  • deepRef (from vue)
  • snapshot.val
  • onValue (from firebase/database)
  • tryOnScopeDispose (from @vueuse/shared)
  • off

update(snapshot: DataSnapshot): void

Code
function update(snapshot: DataSnapshot) {
    data.value = snapshot.val()
  }
  • Parameters:
  • snapshot: DataSnapshot
  • Return Type: void
  • Calls:
  • snapshot.val

Interfaces

UseRTDBOptions

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

Properties

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