I have successfully implemented DynamoDB pagination and tested with API Gateway and Postman. However, I'm confused on how to implement pagination in React frontend.
Currently, upon first call to backend api, it limits to return 2 images, with LastEvaluatedKey. In my React, I implemented React.useEffect() as:
React.useEffect(() => {
async function getAllImages() {
const result = await getImages(auth.getIdToken());
setImages(result.items);
}
try {
getAllImages();
} catch(e) {
alert(`Failed to fetch images ${e.message}`);
}
}, [auth]);
In my api, I implement function to call backend:
export async function getImages(idToken: string): Promise<any> {
const response = await axios.get(`${apiEndpoint}/images`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${idToken}`
}
});
let nextKey = response.data.nextKey;
if (response.data.nextKey === null) {
console.log('No more pictures to load!');
return
} else {
const moreResponse = await axios.get(`${apiEndpoint}/images?nextKey=${nextKey}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${idToken}`
}
});
}
// return response.data.items;
return response.data;
}
Then, I create a "Load More" button to load more images. But I get lost at how to call second call to backend with LastEvaluatedKey to load more images?
Here is my github project: https://github.com/ploratran/DogLookBook/tree/master/client