I have a load more button, within a brand new Next.js app, that uses useSWRInfinite
. Every time I click it it makes 2 calls to my backend. The first call is with index 0 and the second is with the incremented index.
Here is my component:
function Home() {
const { data, size, setSize } = useSWRInfinite(
(index) => `/api/user?page=${index}&per_page=2`,
(url) => fetch(url).then((r) => r.json()),
{ initialSize: 1 }
)
return <button onClick={() => setSize(size + 1)}>{'load more'}</button>
}
And here is my endpoint
export default (req, res) => {
console.log('calling the backend with', req.query)
res.send(200)
}
These are the logs after I've clicked the button 3 times:
calling the backend with { page: '0', per_page: '2' }
calling the backend with { page: '1', per_page: '2' }
calling the backend with { page: '0', per_page: '2' }
calling the backend with { page: '2', per_page: '2' }
calling the backend with { page: '0', per_page: '2' }
calling the backend with { page: '3', per_page: '2' }
Why does it call the backend with page / index 0 and then again with the correct incremented index?