use-axios-client
Basics
Getting Started

Cancelling Requests

You can use the cancel function provided by the hook to cancel an in-flight request.

Common use cases:

  • Allow the user to cancel a request
  • Cancel a pending request when a new request is triggered

Note: Requests are automatically cancelled when a component hosting a hook is unmounted.

# Example

import { useLazyAxios } from 'use-axios-client';
export default () => {
const [saveData, { loading, cancel }] = useLazyAxios({
url: 'https://example/api',
});
return (
<>
<Button disabled={loading} onClick={() => saveData({ name: 'JP' })}>
Save
</Button>
<Button disabled={!loading} onClick={() => cancel()}>
Cancel
</Button>
</>
);
};