Skip to content

Commit 7fd370f

Browse files
authored
fix: isValidating when an error occurred (#1396)
1 parent 3870b0f commit 7fd370f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/use-swr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export const useSWRHandler = <Data = any, Error = any>(
254254
}
255255
} catch (err) {
256256
cleanupState()
257+
cache.set(keyValidating, false)
257258
if (configRef.current.isPaused()) {
258259
setState({
259260
isValidating: false

test/use-swr-error.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,42 @@ describe('useSWR - error', () => {
254254
// error won't be cleared during revalidation
255255
expect(errors).toEqual([null, 'error', 'error'])
256256
})
257+
258+
it('should reset isValidating when an error occured synchronously', async () => {
259+
function Page() {
260+
const { error, isValidating } = useSWR('error-10', () => {
261+
throw new Error('error!')
262+
})
263+
if (error)
264+
return (
265+
<div>
266+
{error.message},{isValidating.toString()}
267+
</div>
268+
)
269+
return <div>hello,{isValidating.toString()}</div>
270+
}
271+
272+
render(<Page />)
273+
screen.getByText('error!,false')
274+
})
275+
276+
it('should reset isValidating when an error occured asynchronously', async () => {
277+
function Page() {
278+
const { error, isValidating } = useSWR('error-11', () =>
279+
createResponse(new Error('error!'))
280+
)
281+
if (error)
282+
return (
283+
<div>
284+
{error.message},{isValidating.toString()}
285+
</div>
286+
)
287+
return <div>hello,{isValidating.toString()}</div>
288+
}
289+
290+
render(<Page />)
291+
screen.getByText('hello,true')
292+
293+
await screen.findByText('error!,false')
294+
})
257295
})

0 commit comments

Comments
 (0)