Skip to content

useRTDB

Category
Export Size
257 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

ts
import { 
useRTDB
} from '@vueuse/firebase/useRTDB'
import {
initializeApp
} from 'firebase/app'
import {
getDatabase
} from 'firebase/database'
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 })

or use createGlobalState from the core package

ts
// store.ts
import { 
createGlobalState
} from '@vueuse/core'
import {
useRTDB
} from '@vueuse/firebase/useRTDB'
export const
useTodos
=
createGlobalState
(
() =>
useRTDB
(db.ref('todos')),
)
vue
<!-- app.vue -->
<script setup lang="ts">
import { 
useTodos
} from './store'
const
todos
=
useTodos
()
</script>

Type Declarations

ts
export interface UseRTDBOptions {
  
errorHandler
?: (
err
: Error) => void
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,
T
| undefined>

Source

SourceDocs

Contributors

Anthony Fu
Robert Soriano
Antério Vieira
SerKo
Anthony Fu
Jelf
Phil Li

Changelog

v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.3.0 on
b5e52 - feat: add errorHandler option (#3232)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access