Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if config.fallback is undefined #2913

Merged
merged 6 commits into from May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})