I want to combine useSWR and useMemo

Asked 2 years ago, Updated 2 years ago, 425 views

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

2022-09-30 22:02

1 Answers

If you want to cache, why don't you use useRef and useEffect?

const previousMaxPage=useRef(maxPage);

useEffect(()=>{
  previousMaxPage.current=maxPage;
}, maxPage);


2022-09-30 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.