Skip to content

Commit c8fd4b6

Browse files
davidstackioposva
andauthored
docs: add fetching data once section (#1323)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent bbb8508 commit c8fd4b6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: docs/guide/realtime-data.md

+24
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ Notice how we rename `data` to whatever makes more sense for the context.
112112
All of the properties that can be defined on the `Ref` are defined as [non-enumerable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) which means they won't be copied over when using the spread operator e.g. `const { data, ...rest } = useDocument(contactSource)`. This is to ensure they are completely ignored and do not create problems in other places like devtools.
113113
:::
114114

115+
### Fetching data once
116+
117+
To fetch data only _once_, pass the [`once`](https://vuefire.vuejs.org/api/interfaces/UseDocumentOptions.html#once) option, which will automatically destroy the subscription as soon as the document or collection is completely fetched:
118+
119+
<FirebaseExample>
120+
121+
```ts{3,4}
122+
import { useDatabaseList, useDatabaseObject } from 'vuefire'
123+
import { ref as dbRef } from 'firebase/database'
124+
125+
const todos = useDatabaseList(dbRef(db, 'todos'), { once: true })
126+
const someTodo = useDatabaseObject(dbRef(db, 'todos', 'someId'), { once: true })
127+
```
128+
129+
```ts{3,4}
130+
import { useCollection, useDocument } from 'vuefire'
131+
import { collection, doc } from 'firebase/firestore'
132+
133+
const todos = useCollection(collection(db, 'todos'), { once: true })
134+
const someTodo = useDocument(doc(collection(db, 'todos'), 'someId'), { once: true })
135+
```
136+
137+
</FirebaseExample>
138+
115139
## VueFire additions
116140

117141
VueFire adds a few properties to the data snapshot to make it easier to work with.

0 commit comments

Comments
 (0)