Skip to content

useFirestore

Category
Export Size
667 B
Package
@vueuse/firebase
Last Changed
4 months ago

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

ts
import { 
useFirestore
} from '@vueuse/firebase/useFirestore'
import {
initializeApp
} from 'firebase/app'
import {
collection
,
doc
,
getFirestore
,
limit
,
orderBy
,
query
} from 'firebase/firestore'
import {
computed
,
shallowRef
} from 'vue'
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
postsLimit
=
shallowRef
(10)
const
postsQuery
=
computed
(() =>
query
(
collection
(
db
, 'posts'),
orderBy
('createdAt', 'desc'),
limit
(
postsLimit
.
value
)))
const
posts
=
useFirestore
(
postsQuery
)
// you can use the boolean value to tell a query when it is ready to run // when it gets falsy value, return the initial value const
userId
=
shallowRef
('')
const
userQuery
=
computed
(() =>
userId
.
value
&&
doc
(
db
, 'users',
userId
.
value
))
const
userData
=
useFirestore
(
userQuery
, null)

Share across instances

You can reuse the db reference by passing autoDispose: false. You can also set an amount of milliseconds before auto disposing the db reference.

Note : Getting a not disposed db reference again don't cost a Firestore read.

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

or use createGlobalState from the core package

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

Type Declarations

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

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
Kiyohiko Heima
IlyaL
Zehir
Antério Vieira
SerKo
azaleta
sun0day
Alex Kozack
Rodolfo Román
Matthias Stiller
Phil Li

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

FREE WEEKEND
Unlimited access to ALL Vue School courses
8-9 November 2025
Reserve Your Spot
8-9 November 2025