Data from APIs are commonly paginated. We also have often use cases when we have to fetch all the records from an API. For example, when we want to make data visualizations. Therefore, we have to iteratively fetch all the pages which contain the data we need.
const [results, fetchData] = useRecursiveFetchPaginate<
ProductsResponse
>(getAllProductsByParam, null, {
debug: true,
until({ total }) {
return total >= 1000
},
callback(products) {
console.log('callback data', products)
},
finally(products) {
console.log('results', products)
},
})
// Call
useEffect(fetchData, [fetchData])
The structure of the call stack is evident when looking at the network requests after running the function.
Download the latest use-recursive-fetch-paginate from GitHub, or install with npm:
npm install use-recursive-fetch-paginate
This program is free software; it is distributed under an MIT License.