Next.js implements pageation.
There is something you would like to see on page {page} / {max_page} page.
We calculate max_page as Math.floor after fetching data in SWR.
As for max_page, the value does not change even after pageation transition, so I want to fix it, but every time I click on the page number, max_page is recalculated once, so the display is glimmering.
Conversely, if you try to fix the max_page part using useMemo, the initial value is calculated at the timing of useSWR fetch acquisition, so the initial value does not fit well (fixed to a value of 0 before fetch acquisition).
I'd like to get the number of data in useSWR, set the max_page calculated from the number of data to the initial value, and cache it. Is there a good way to write it?
reactjs next.js
If you want to cache, why don't you use useRef
and useEffect
?
const previousMaxPage=useRef(maxPage);
useEffect(()=>{
previousMaxPage.current=maxPage;
}, maxPage);
© 2024 OneMinuteCode. All rights reserved.