mirror of
https://github.com/gosticks/react-table.git
synced 2025-10-16 11:55:36 +00:00
Upgrade controlled pagination example for deduped fetching
This commit is contained in:
parent
f1ad032587
commit
227f7905b0
@ -240,26 +240,33 @@ function App() {
|
||||
const [data, setData] = React.useState([]);
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
const [pageCount, setPageCount] = React.useState(0);
|
||||
const fetchIdRef = React.useRef(0);
|
||||
|
||||
const fetchData = React.useCallback(({ pageSize, pageIndex }) => {
|
||||
// This will get called when the table needs new data
|
||||
// You could fetch your data from literally anywhere,
|
||||
// even a server. But for this example, we'll just fake it.
|
||||
|
||||
// Give this fetch an ID
|
||||
const fetchID = ++fetchIdRef.current;
|
||||
|
||||
// Set the loading state
|
||||
setLoading(true);
|
||||
|
||||
// We'll even set a delay to simulate a server here
|
||||
setTimeout(() => {
|
||||
const startRow = pageSize * pageIndex;
|
||||
const endRow = startRow + pageSize;
|
||||
setData(serverData.slice(startRow, endRow));
|
||||
// Only update the data if this is the latest fetch
|
||||
if (fetchID === fetchIdRef.current) {
|
||||
const startRow = pageSize * pageIndex;
|
||||
const endRow = startRow + pageSize;
|
||||
setData(serverData.slice(startRow, endRow));
|
||||
|
||||
// Your server could send back total page count.
|
||||
// For now we'll just fake it, too
|
||||
setPageCount(Math.ceil(serverData.length / pageSize));
|
||||
// Your server could send back total page count.
|
||||
// For now we'll just fake it, too
|
||||
setPageCount(Math.ceil(serverData.length / pageSize));
|
||||
|
||||
setLoading(false);
|
||||
setLoading(false);
|
||||
}
|
||||
}, 1000);
|
||||
}, []);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user