Skip to content

Commit 26abb06

Browse files
authored
docs(firestore): added pagination using firestore
1 parent 6b509aa commit 26abb06

File tree

5 files changed

+171
-2
lines changed

5 files changed

+171
-2
lines changed

.spellcheck.dict.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Firebase
6262
FirebaseApp
6363
firebase-ios-sdk
6464
Firestore
65+
firestore
6566
getIdToken
6667
GDPR
6768
GDPR-compliant
@@ -89,6 +90,7 @@ Javascript
8990
javascript
9091
JS
9192
JSON
93+
lastDocument
9294
launchProperties
9395
learnt
9496
Lerna
@@ -171,6 +173,7 @@ uncomment
171173
unhandled
172174
unsubscriber
173175
untampered
176+
userData
174177
utils
175178
Utils
176179
v5

docs/firestore/pagination.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: Pagination
3+
description: Pagination example using Cloud Firestore.
4+
next: /functions/usage
5+
previous: /firestore/usage-with-flatlists
6+
---
7+
8+
Pagination using cloud firestore may be done in various ways but here's a basic way to do it using the firestore query features:
9+
[`orderBy`, `limit`, `startAfter`]
10+
11+
# Setup state
12+
13+
First, create a list display component with 2 state items; `lastDocument` and `userData`:
14+
15+
```jsx
16+
import React, { useState } from 'react';
17+
import type { Node } from 'react';
18+
import { Text, View, Button, Alert } from 'react-native';
19+
20+
import firestore from '@react-native-firebase/firestore';
21+
22+
const userCollection = firestore().collection('Users');
23+
24+
const App: () => Node = () => {
25+
const [lastDocument, setLastDocument] = useState();
26+
const [userData, setUserData] = useState([]);
27+
};
28+
```
29+
30+
# `LoadData` function
31+
32+
Next, make a function called `LoadData` that fetches data from `Users` collection, and call it when a `Button` is pressed.
33+
34+
If lastDocument is not assigned (meaning initial load), the function will fetch from the start.
35+
After successful fetch from the collection, store the last snapshot data by `setLastDocument`.
36+
37+
```jsx
38+
import React, { useState } from 'react';
39+
import type { Node } from 'react';
40+
import { Text, View, Button, Alert } from 'react-native';
41+
42+
import firestore from '@react-native-firebase/firestore';
43+
44+
const userCollection = firestore().collection('Users');
45+
46+
const App: () => Node = () => {
47+
const [lastDocument, setLastDocument] = useState();
48+
const [userData, setUserData] = useState([]);
49+
50+
function LoadData() {
51+
console.log('LOAD');
52+
let query = userCollection.orderBy('age'); // sort the data
53+
if (lastDocument !== undefined) {
54+
query = query..startAfter(lastDocument); // fetch data following the last document accessed
55+
}
56+
query.limit(3) // limit to your page size, 3 is just an example
57+
.get()
58+
.then(querySnapshot => {
59+
setLastDocument(querySnapshot.docs[querySnapshot.docs.length - 1]);
60+
MakeUserData(querySnapshot.docs);
61+
});
62+
}
63+
}
64+
65+
return (
66+
<View>
67+
{userData}
68+
<Button
69+
onPress={() => {
70+
LoadData();
71+
}}
72+
title="Load Next"
73+
/>
74+
</View>
75+
);
76+
};
77+
```
78+
79+
# `MakeUserData` function
80+
81+
This is just an example function, alter it to process the data to meet your requirements.
82+
In this specific example, it will replace the userData component with the new data fetched.
83+
84+
```js
85+
function MakeUserData(docs) {
86+
let templist = []; //[...userData] <- use this instead of [] if you want to save the previous data.
87+
docs.forEach((doc, i) => {
88+
console.log(doc._data);
89+
let temp = (
90+
<View key={i} style={{ margin: 10 }}>
91+
<Text>{doc._data.name}</Text>
92+
<Text>{doc._data.age}</Text>
93+
</View>
94+
);
95+
templist.push(temp);
96+
});
97+
setUserData(templist); //replace with the new data
98+
}
99+
```
100+
101+
Now, every time the button is pressed, `Users` collection data will be fetched one page at a time.
102+
103+
# Conclusion
104+
105+
Here's the full example code
106+
107+
```jsx
108+
import React, { useState } from 'react';
109+
import type { Node } from 'react';
110+
import { Text, View, Button, Alert } from 'react-native';
111+
112+
import firestore from '@react-native-firebase/firestore';
113+
114+
const userCollection = firestore().collection('Users');
115+
116+
const App: () => Node = () => {
117+
const [lastDocument, setLastDocument] = useState();
118+
const [userData, setUserData] = useState([]);
119+
120+
function LoadData() {
121+
console.log('LOAD');
122+
let query = userCollection.orderBy('age'); // sort the data
123+
if (lastDocument !== undefined) {
124+
query = query..startAfter(lastDocument); // fetch data following the last document accessed
125+
}
126+
query.limit(3) // limit to your page size, 3 is just an example
127+
.get()
128+
.then(querySnapshot => {
129+
setLastDocument(querySnapshot.docs[querySnapshot.docs.length - 1]);
130+
MakeUserData(querySnapshot.docs);
131+
});
132+
}
133+
}
134+
135+
function MakeUserData(docs) {
136+
let templist = [];
137+
docs.forEach((doc, i) => {
138+
console.log(doc._data);
139+
let temp = (
140+
<View key={i} style={{ margin: 10 }}>
141+
<Text>{doc._data.name}</Text>
142+
<Text>{doc._data.age}</Text>
143+
</View>
144+
);
145+
templist.push(temp);
146+
});
147+
setUserData(templist);
148+
}
149+
150+
return (
151+
<View>
152+
{userData}
153+
<Button
154+
onPress={() => {
155+
LoadData();
156+
}}
157+
title="Load Next"
158+
/>
159+
</View>
160+
);
161+
};
162+
163+
export default App;
164+
```

docs/firestore/usage-with-flatlists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Usage with FlatLists
33
description: Using Cloud Firestore collections with FlatLists.
4-
next: /functions/usage
4+
next: /firestore/pagination
55
previous: /firestore/emulator
66
---
77

docs/functions/usage/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Cloud Functions
33
description: Installation and getting started with Cloud Functions.
44
icon: //static.invertase.io/assets/firebase/cloud-functions.svg
55
next: /functions/writing-deploying-functions
6-
previous: /firestore/usage-with-flatlists
6+
previous: /firestore/pagination
77
---
88

99
# Installation

docs/sidebar.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
- '/firestore/emulator'
4747
- - Usage with FlatLists
4848
- '/firestore/usage-with-flatlists'
49+
- - Implementing Pagination
50+
- '/firestore/pagination'
4951
- - Building a "TODO" app
5052
- 'https://invertase.io/blog/getting-started-with-cloud-firestore-on-react-native'
5153
- '//static.invertase.io/assets/firebase/cloud-firestore.svg'

0 commit comments

Comments
 (0)