|
| 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 | +``` |
0 commit comments