diff --git a/src/core/use-swr.ts b/src/core/use-swr.ts index 8b6644f284..9f8648c60d 100644 --- a/src/core/use-swr.ts +++ b/src/core/use-swr.ts @@ -144,7 +144,11 @@ export const useSWRHandler = ( // 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) } diff --git a/test/use-swr-config.test.tsx b/test/use-swr-config.test.tsx index 87ac30a5d0..ce87a45d8b 100644 --- a/test/use-swr-config.test.tsx +++ b/test/use-swr-config.test.tsx @@ -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
data: {data}
+ } + + renderWithConfig(, { + fetcher, + fallback: undefined + }) + // hydration + screen.getByText('data:') + // mount + await screen.findByText('data: data') + }) })