From 4469c0c72c9c04abd7c5ce687e2fed75502f8cd9 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 4 Apr 2021 16:45:08 +0800 Subject: [PATCH 1/2] fix type: allow getKey to be null --- src/types.ts | 7 +++---- src/use-swr-infinite.ts | 4 ++-- test/use-swr-infinite.test.tsx | 11 +++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/types.ts b/src/types.ts index 9cda764f6..031749948 100644 --- a/src/types.ts +++ b/src/types.ts @@ -129,10 +129,9 @@ export interface SWRResponse { isValidating: boolean } -export type KeyLoader = ( - index: number, - previousPageData: Data | null -) => ValueKey +export type KeyLoader = + | ((index: number, previousPageData: Data | null) => ValueKey) + | null /** * @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`. diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index b1c9b2ed5..68ddbc2d9 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -44,7 +44,7 @@ function useSWRInfinite( // get the serialized key of the first page let firstPageKey: string | null = null try { - ;[firstPageKey] = cache.serializeKey(getKey(0, null)) + ;[firstPageKey] = cache.serializeKey(getKey ? getKey(0, null) : null) } catch (err) { // not ready } @@ -104,7 +104,7 @@ function useSWRInfinite( let previousPageData = null for (let i = 0; i < pageSize; ++i) { const [pageKey, pageArgs] = cache.serializeKey( - getKey(i, previousPageData) + getKey ? getKey(i, previousPageData) : null ) if (!pageKey) { diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 76a173abf..2531c2e39 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -543,4 +543,15 @@ describe('useSWRInfinite', () => { await screen.findByText('A:page-2-2') await screen.findByText('B:page-2-2') }) + + it.only('should support null as getKey', async () => { + function Page() { + const { data } = useSWRInfinite(null, () => 'data') + return
data:{data}
+ } + + render() + screen.getByText('data:') + await screen.findByText('data:') + }) }) From 25429319e8bfb6a861e651d7df6aab82b00590a4 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 4 Apr 2021 16:59:00 +0800 Subject: [PATCH 2/2] fix leaking internal state when setSize on null key --- src/use-swr-infinite.ts | 6 ++++++ test/use-swr-infinite.test.tsx | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index 68ddbc2d9..971125321 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -159,6 +159,9 @@ function useSWRInfinite( const mutate = useCallback( (data: MutatorCallback, shouldRevalidate = true) => { + // It is possible that the key is still falsy. + if (!contextCacheKey) return undefined + if (shouldRevalidate && typeof data !== 'undefined') { // we only revalidate the pages that are changed const originalData = dataRef.current @@ -178,6 +181,9 @@ function useSWRInfinite( // extend the SWR API const setSize = useCallback( (arg: number | ((size: number) => number)) => { + // It is possible that the key is still falsy. + if (!pageSizeCacheKey) return undefined + let size if (typeof arg === 'function') { size = arg(resolvePageSize()) diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 2531c2e39..abffd974c 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -544,14 +544,31 @@ describe('useSWRInfinite', () => { await screen.findByText('B:page-2-2') }) - it.only('should support null as getKey', async () => { + it('should support null as getKey', async () => { function Page() { - const { data } = useSWRInfinite(null, () => 'data') - return
data:{data}
+ const { data, setSize } = useSWRInfinite( + null, + () => 'data' + ) + + return ( +
{ + // load next page + setSize(size => size + 1) + }} + > + data:{data || ''} +
+ ) } render() screen.getByText('data:') await screen.findByText('data:') + + // load next page + fireEvent.click(screen.getByText('data:')) + await screen.findByText('data:') }) })