A React hook for canceling Axios requests using the AbortController API (newer, recommended) or legacy Axios cancel tokens (deprecated). Version 1.0.3, maintained, ships TypeScript types. Key differentiator: simplifies request cancellation in React components with a single hook, providing abort signal/cancel token generation and automatic cleanup. Compared to raw AbortController or Axios CancelToken, it integrates directly with React's lifecycle.
npm install react-use-cancel-tokenNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates using useAbortController to cancel previous Axios requests on re-click, with AbortSignal and error checking.
Replace import { useCancelToken } with import useAbortController from 'react-use-cancel-token' and use newAbortSignal() instead of newCancelToken().Add a useEffect that calls cancelPreviousRequest() on cleanup to prevent memory leaks.
Call cancelPreviousRequest() right before the axios request (as shown in docs) to avoid race conditions.
Only call cancelPreviousRequest from within a React side effect (useEffect, event handler) or after the component is mounted.
Switch to AbortController usage: useAbortController instead of useCancelToken, and pass signal instead of cancelToken.
Change import to: import useAbortController from 'react-use-cancel-token'
Run npm install react-use-cancel-token, and ensure import path is correct.
Add a mounted ref or use the abortsignal in a useEffect cleanup to avoid state updates.
Ensure you call newCancelToken() before each request, or update to useAbortController.