Skip to content
On this page

useFirestore

Reactive Firestore binding. Making it straightforward to always keep your local data in sync with remotes databases.

Available in the @vueuse/firebase add-on.

Usage









 


 




 


















import { computed, ref } from 'vue'
import { initializeApp } from 'firebase/app'
import { collection, doc, getFirestore, limit, orderBy, query } from 'firebase/firestore'
import { useFirestore } from '@vueuse/firebase/useFirestore'

const app = initializeApp({ projectId: 'MY PROJECT ID' })
const db = getFirestore(app)

const todos = useFirestore(collection(db, 'todos'))

// or for doc reference
const user = useFirestore(doc(db, 'users', 'my-user-id'))

// you can also use ref value for reactive query
const postLimit = ref(10)
const postsQuery = computed(() => query(collection(db, 'posts'), orderBy('createdAt', 'desc'), limit(postLimit.value)))
const posts = useFirestore(postsQuery)
import { computed, ref } from 'vue'
import { initializeApp } from 'firebase/app'
import { collection, doc, getFirestore, limit, orderBy, query } from 'firebase/firestore'
import { useFirestore } from '@vueuse/firebase/useFirestore'

const app = initializeApp({ projectId: 'MY PROJECT ID' })
const db = getFirestore(app)

const todos = useFirestore(collection(db, 'todos'))

// or for doc reference
const user = useFirestore(doc(db, 'users', 'my-user-id'))

// you can also use ref value for reactive query
const postLimit = ref(10)
const postsQuery = computed(() => query(collection(db, 'posts'), orderBy('createdAt', 'desc'), limit(postLimit.value)))
const posts = useFirestore(postsQuery)

Share across instances

You can reuse the db reference by passing autoDispose: false

const todos = useFirestore(collection(db, 'todos'), undefined, { autoDispose: false })
const todos = useFirestore(collection(db, 'todos'), undefined, { autoDispose: false })

or use createGlobalStatefrom the core package

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

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

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

export default {
  setup() {
    const todos = useTodos()
    return { todos }
  },
}
// app.js
import { useTodos } from './store'

export default {
  setup() {
    const todos = useTodos()
    return { todos }
  },
}

Type Declarations

export interface UseFirestoreOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean
}
export declare type FirebaseDocRef<T> = Query<T> | DocumentReference<T>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T>>,
  initialValue: T,
  options?: UseFirestoreOptions
): Ref<T | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T>>,
  initialValue: T[],
  options?: UseFirestoreOptions
): Ref<T[]>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T>>,
  initialValue?: T | undefined,
  options?: UseFirestoreOptions
): Ref<T | undefined | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T>>,
  initialValue?: T[],
  options?: UseFirestoreOptions
): Ref<T[] | undefined>
export interface UseFirestoreOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean
}
export declare type FirebaseDocRef<T> = Query<T> | DocumentReference<T>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T>>,
  initialValue: T,
  options?: UseFirestoreOptions
): Ref<T | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T>>,
  initialValue: T[],
  options?: UseFirestoreOptions
): Ref<T[]>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T>>,
  initialValue?: T | undefined,
  options?: UseFirestoreOptions
): Ref<T | undefined | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T>>,
  initialValue?: T[],
  options?: UseFirestoreOptions
): Ref<T[] | undefined>

Source

Category
Package
@vueuse/firebase
Last Changed
4 weeks ago

SourceDocs

Contributors

Anthony Fu
Antério Vieira
Kiyohiko Heima
Alex Kozack
Rodolfo Román
Matthias Stiller
Phil Li

Changelog

v9.0.1 on 7/29/2022
ffb24 - feat: support reactive query (#2008)
v9.0.0-beta.2 on 7/24/2022
9c65f - feat(firebase)!: support firebase 9

Released under the MIT License.