Skip to content

Instantly share code, notes, and snippets.

@joeljerushan
Last active April 13, 2024 06:11
Show Gist options
  • Save joeljerushan/e931f5ee4a4ab3664bbd47d1b06b7264 to your computer and use it in GitHub Desktop.
Save joeljerushan/e931f5ee4a4ab3664bbd47d1b06b7264 to your computer and use it in GitHub Desktop.
React Pagination with Firebase FireStore - (Prev / Next Pagination)
import React, { useState, useEffect } from 'react'
//i'm using react-bootstrap for UI elements
import { Table, Button, ButtonGroup } from "react-bootstrap";
//firebase config
import firebase from './../../Firebase'
export default function App() {
const [list, setList] = useState([]);
const [page, setPage] = useState(1);
useEffect(() => {
const fetchData = async () => {
await firebase.firestore().collection('users')
.orderBy('created', 'desc')
.limit(5)
.onSnapshot(function(querySnapshot) {
var items = [];
querySnapshot.forEach(function(doc) {
items.push({ key: doc.id, ...doc.data() });
});
console.log('first item ', items[0])
setList(items);
})
};
fetchData();
}, []);
const showNext = ({ item }) => {
if(list.length === 0) {
alert("Thats all we have for now !")
} else {
const fetchNextData = async () => {
await firebase.firestore().collection('users')
.orderBy('created', 'desc')
.limit(5)
.startAfter(item.created)
.onSnapshot(function(querySnapshot) {
const items = [];
querySnapshot.forEach(function(doc) {
items.push({ key: doc.id, ...doc.data() });
});
setList(items);
setPage(page + 1)
})
};
fetchNextData();
}
};
const showPrevious = ({item}) => {
const fetchPreviousData = async () => {
await firebase.firestore().collection('users')
.orderBy('created', 'desc')
.endBefore(item.created)
.limitToLast(5)
.onSnapshot(function(querySnapshot) {
const items = [];
querySnapshot.forEach(function(doc) {
items.push({ key: doc.id, ...doc.data() });
});
setList(items);
setPage(page - 1)
})
};
fetchPreviousData();
};
return (
<>
{
//list doc's here
list.map((doc) => (
<tr key={doc.key}>
<td>{ doc.name }</td>
<td>{ doc.age }</td>
<td>{ doc.note }</td>
</tr>
))
}
<ButtonGroup>
{
//show previous button only when we have items
page === 1 ? '' :
<Button onClick={() => showPrevious({ item: list[0] }) }>Previous</Button>
}
{
//show next button only when we have items
list.length < 5 ? '' :
<Button onClick={() => showNext({ item: list[list.length - 1] })}>Next</Button>
}
</ButtonGroup>
</>
)}
</>
)
}
@sthadevraj
Copy link

amazing.keep it up...

@awoodsmedia
Copy link

Thanks so much!

@jmy2022
Copy link

jmy2022 commented Aug 29, 2023

Odd numbers have errors

@mohaahadji
Copy link

That was very helpful Thanks!

@longnguyen2508
Copy link

I'm wondering how to unsubscribe after going to the next and previous pages.

@BiancaHoffer
Copy link

Thank you for this! It's the only way I found on the internet to implement pagination with Firestore using "next" and "previous" buttons! I used the code a bit differently, but followed your logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment