Skip to content

Commit ad7523a

Browse files
author
Lukas Kral
committed
fix: autoCancel respected when hook is unmounted
1 parent d42b3a5 commit ad7523a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ export function makeUseAxios(configureOptions) {
251251
request(withCancelToken(config), options, dispatch).catch(() => {})
252252
}
253253

254-
return cancelOutstandingRequest
254+
return () => {
255+
if (options.autoCancel) {
256+
cancelOutstandingRequest()
257+
}
258+
}
255259
}, [config, options, withCancelToken, cancelOutstandingRequest])
256260

257261
const refetch = React.useCallback(

test/index.test.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ function standardTests(
387387
expect(cancel).not.toHaveBeenCalled()
388388
})
389389

390+
it('should skip default cancellation after unmount if options.autoCancel is set to false', async () => {
391+
axios.mockResolvedValue({ data: 'whatever' })
392+
393+
const { waitForNextUpdate, unmount } = setup('', {
394+
autoCancel: false
395+
})
396+
397+
await waitForNextUpdate()
398+
399+
unmount()
400+
401+
expect(cancel).not.toHaveBeenCalled()
402+
})
403+
390404
it('should provide the cancel token to axios', async () => {
391405
axios.mockResolvedValueOnce({ data: 'whatever' })
392406

0 commit comments

Comments
 (0)