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 createGlobalState
from 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
Contributors
Anthony Fu
Antério Vieira
Kiyohiko Heima
Alex Kozack
Rodolfo Román
Matthias Stiller
Phil Li