Skip to content

Commit 1882cbd

Browse files
authored
Fix page size persistence when remounting (vercel#495)
* fix page size persistence * fix version
1 parent e415321 commit 1882cbd

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/use-swr-infinite.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,16 @@ function useSWRInfinite<Data = any, Error = any>(
106106
cachedPageSize = cache.get(pageCountCacheKey)
107107
}
108108
const pageCountRef = useRef<number>(cachedPageSize || initialSize)
109+
const didMountRef = useRef<boolean>(false)
109110

110111
// every time the key changes, we reset the page size if it's not persisted
111112
useEffect(() => {
112-
if (!persistSize) {
113-
pageCountRef.current = initialSize
113+
if (didMountRef.current) {
114+
if (!persistSize) {
115+
pageCountRef.current = initialSize
116+
}
117+
} else {
118+
didMountRef.current = true
114119
}
115120
}, [firstPageKey])
116121

test/use-swr-infinite.test.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('useSWRInfinite', () => {
306306
expect(container.textContent).toMatchInlineSnapshot(`"page 0, "`)
307307
})
308308

309-
it('should presist page size when key changes', async () => {
309+
it('should persist page size when key changes', async () => {
310310
let toggle
311311

312312
function Page() {
@@ -351,4 +351,55 @@ describe('useSWRInfinite', () => {
351351
await act(() => new Promise(res => setTimeout(res, 250)))
352352
expect(container.textContent).toMatchInlineSnapshot(`"page 0, page 1, "`)
353353
})
354+
355+
it('should persist page size when remount', async () => {
356+
let toggle
357+
358+
function Comp() {
359+
const { data, size, setSize } = useSWRInfinite<string, string>(
360+
index => [`pagetest-8`, index],
361+
async (_, index) => {
362+
await new Promise(res => setTimeout(res, 100))
363+
return `page ${index}, `
364+
}
365+
)
366+
367+
return (
368+
<div
369+
onClick={() => {
370+
// load next page
371+
setSize(size + 1)
372+
}}
373+
>
374+
{data}
375+
</div>
376+
)
377+
}
378+
379+
function Page() {
380+
const [show, setShow] = useState(true)
381+
toggle = setShow
382+
return show ? <Comp /> : null
383+
}
384+
385+
const { container } = render(<Page />)
386+
expect(container.textContent).toMatchInlineSnapshot(`""`)
387+
await waitForDomChange({ container }) // mount
388+
expect(container.textContent).toMatchInlineSnapshot(`"page 0, "`)
389+
390+
// load next page
391+
fireEvent.click(container.firstElementChild)
392+
await act(() => new Promise(res => setTimeout(res, 150)))
393+
expect(container.textContent).toMatchInlineSnapshot(`"page 0, page 1, "`)
394+
395+
// pages should be unmounted now
396+
act(() => toggle(v => !v))
397+
await act(() => new Promise(res => setTimeout(res, 50)))
398+
expect(container.textContent).toMatchInlineSnapshot(`""`)
399+
400+
// remount, should still have 2 pages
401+
act(() => toggle(v => !v))
402+
await act(() => new Promise(res => setTimeout(res, 150)))
403+
expect(container.textContent).toMatchInlineSnapshot(`"page 0, page 1, "`)
404+
})
354405
})

0 commit comments

Comments
 (0)