How to use getKey?
#2025
-
Hi, I don't know how to use limit and pageIndex. Now fetcer fetches the data for all the books. const limit = 5;
const getKey = (pageIndex: number, previousPageData: any) => {
if (previousPageData && !previousPageData.length) return null;
return `http://localhost:3000/api/books?page=${
pageIndex + 1
}&limit=${limit}`;
};
const { data, size, setSize } = useSWRInfinite(
getKey,
(url) =>
fetch(url, {
method: 'GET',
}).then((r) => r.json()),
{
initialSize: 1,
}
);
|
Beta Was this translation helpful? Give feedback.
Answered by
nehiro
Jun 23, 2022
Replies: 1 comment
-
Resolved. I did not know how to use slice(). if (page > 1) {
const result = data.slice(limit * page - limit, limit * page);
return res.status(200).json(result);
} else {
const result = data.slice(0, limit);
return res.status(200).json(result);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nehiro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resolved. I did not know how to use slice().