Skip to content

Commit 825092b

Browse files
authored
fix: Fix #4221 (#4223)
fix #4221
1 parent acd241e commit 825092b

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/index/use-swr.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,13 @@ export const useSWRHandler = <Data = any, Error = any>(
376376
return isUndefined(data) || revalidateIfStale
377377
})()
378378

379+
const defaultValidatingState = isInitialMount && shouldDoInitialRevalidation
380+
379381
const isValidating = isUndefined(cached.isValidating)
380-
? shouldDoInitialRevalidation
382+
? defaultValidatingState
381383
: cached.isValidating
382384
const isLoading = isUndefined(cached.isLoading)
383-
? shouldDoInitialRevalidation
385+
? defaultValidatingState
384386
: cached.isLoading
385387

386388
// The revalidation function is a carefully crafted wrapper of the original

test/use-swr-local-mutation.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,39 @@ describe('useSWR - local mutation', () => {
791791
screen.getByText('false')
792792
})
793793

794+
it('isLoading and isValidating should be false when revalidate is set to false', async () => {
795+
const key = createKey()
796+
function Page() {
797+
const { data, isLoading, isValidating, mutate } = useSWR(
798+
key,
799+
() => 'data',
800+
{
801+
fallbackData: 'fallback',
802+
revalidateOnMount: false,
803+
revalidateOnFocus: false
804+
}
805+
)
806+
return (
807+
<div>
808+
<p>data: {data}</p>
809+
<p>isLoading: {isLoading.toString()}</p>
810+
<p>isValidating: {isValidating.toString()}</p>
811+
<button onClick={() => mutate('fallback1', { revalidate: false })}>
812+
mutate
813+
</button>
814+
</div>
815+
)
816+
}
817+
renderWithConfig(<Page />)
818+
screen.getByText('data: fallback')
819+
screen.getByText('isLoading: false')
820+
screen.getByText('isValidating: false')
821+
fireEvent.click(screen.getByText('mutate'))
822+
await screen.findByText('data: fallback1')
823+
screen.getByText('isLoading: false')
824+
screen.getByText('isValidating: false')
825+
})
826+
794827
it('bound mutate should always use the latest key', async () => {
795828
const key = createKey()
796829
const fetcher = jest.fn(() => 'data')

0 commit comments

Comments
 (0)