Skip to content

Commit 2bc18a2

Browse files
committed
fix(loading): Prevent isLoading from being true with fallbackData
When `fallbackData` is provided, `isLoading` should not be true initially. Instead, `isValidating` should be true to indicate background revalidation. This prevents a "flash of skeleton" UI when initial data is available. Fixes vercel#3046
1 parent 92a5cfc commit 2bc18a2

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/index/use-swr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const useSWRHandler = <Data = any, Error = any>(
195195

196196
return {
197197
isValidating: true,
198-
isLoading: true,
198+
isLoading: isUndefined(fallback),
199199
...snapshot
200200
}
201201
}
@@ -278,8 +278,8 @@ export const useSWRHandler = <Data = any, Error = any>(
278278

279279
const returnedData = keepPreviousData
280280
? isUndefined(cachedData)
281-
// checking undefined to avoid null being fallback as well
282-
? isUndefined(laggyDataRef.current)
281+
? // checking undefined to avoid null being fallback as well
282+
isUndefined(laggyDataRef.current)
283283
? data
284284
: laggyDataRef.current
285285
: cachedData
@@ -390,7 +390,7 @@ export const useSWRHandler = <Data = any, Error = any>(
390390
const initialState: State<Data, Error> = { isValidating: true }
391391
// It is in the `isLoading` state, if and only if there is no cached data.
392392
// This bypasses fallback data and laggy data.
393-
if (isUndefined(getCache().data)) {
393+
if (isUndefined(getCache().data) && isUndefined(fallback)) {
394394
initialState.isLoading = true
395395
}
396396
try {

0 commit comments

Comments
 (0)