Skip to content

Commit

Permalink
fix: check if config.fallback is undefined (#2913)
Browse files Browse the repository at this point in the history
* fix: check if config.fallback is undefined

* chore: use UNDEFINED

* test: test for config.fallback is undefined
  • Loading branch information
taku-hatano committed May 8, 2024
1 parent 5c34bfb commit 2a5000e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/use-swr.ts
Expand Up @@ -144,7 +144,11 @@ export const useSWRHandler = <Data = any, Error = any>(

// Resolve the fallback data from either the inline option, or the global provider.
// If it's a promise, we simply let React suspend and resolve it for us.
let fallback = isUndefined(fallbackData) ? config.fallback[key] : fallbackData
let fallback = isUndefined(fallbackData)
? isUndefined(config.fallback)
? UNDEFINED
: config.fallback[key]
: fallbackData
if (fallback && isPromiseLike(fallback)) {
fallback = use(fallback)
}
Expand Down
19 changes: 19 additions & 0 deletions test/use-swr-config.test.tsx
Expand Up @@ -184,4 +184,23 @@ describe('useSWR - configs', () => {
expect(config.fallback).toEqual({ a: 2, c: 2 })
expect(config.use).toEqual([middleware2])
})

it('should not occur error when fallback is undefined', async () => {
const key = createKey()
const fetcher = () => 'data'

function Page() {
const { data } = useSWR(key)
return <div>data: {data}</div>
}

renderWithConfig(<Page />, {
fetcher,
fallback: undefined
})
// hydration
screen.getByText('data:')
// mount
await screen.findByText('data: data')
})
})

0 comments on commit 2a5000e

Please sign in to comment.