Skip to content

Commit

Permalink
Bug fixes (#1096)
Browse files Browse the repository at this point in the history
* fix type: allow getKey to be null

* fix leaking internal state when setSize on null key
  • Loading branch information
shuding committed Apr 4, 2021
1 parent ad17494 commit 7185877
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ export interface SWRResponse<Data, Error> {
isValidating: boolean
}

export type KeyLoader<Data = any> = (
index: number,
previousPageData: Data | null
) => ValueKey
export type KeyLoader<Data = any> =
| ((index: number, previousPageData: Data | null) => ValueKey)
| null

/**
* @deprecated `SWRInfiniteConfigInterface` will be renamed to `SWRInfiniteConfiguration`.
Expand Down
10 changes: 8 additions & 2 deletions src/use-swr-infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function useSWRInfinite<Data = any, Error = any>(
// 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
}
Expand Down Expand Up @@ -104,7 +104,7 @@ function useSWRInfinite<Data = any, Error = any>(
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) {
Expand Down Expand Up @@ -159,6 +159,9 @@ function useSWRInfinite<Data = any, Error = any>(

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
Expand All @@ -178,6 +181,9 @@ function useSWRInfinite<Data = any, Error = any>(
// 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())
Expand Down
28 changes: 28 additions & 0 deletions test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,32 @@ describe('useSWRInfinite', () => {
await screen.findByText('A:page-2-2')
await screen.findByText('B:page-2-2')
})

it('should support null as getKey', async () => {
function Page() {
const { data, setSize } = useSWRInfinite<string, string>(
null,
() => 'data'
)

return (
<div
onClick={() => {
// load next page
setSize(size => size + 1)
}}
>
data:{data || ''}
</div>
)
}

render(<Page />)
screen.getByText('data:')
await screen.findByText('data:')

// load next page
fireEvent.click(screen.getByText('data:'))
await screen.findByText('data:')
})
})

0 comments on commit 7185877

Please sign in to comment.