This content originally appeared on Bram.us and was authored by Bramus!
A React Hook that wraps around the almighty AbortController
. It will automatically call abort()
on the created AbortSignal
instance when the component unmounts.
import useUnmountSignal from 'use-unmount-signal';
function Example() {
const unmountSignal = useUnmountSignal();
return (
<button
onClick={() =>
fetch('https://ping.example.com', { signal: unmountSignal })
}>
Ping
</button>
);
}
Installation per NPM:
npm i use-unmount-signal
This content originally appeared on Bram.us and was authored by Bramus!

Bramus! | Sciencx (2021-07-27T23:09:38+00:00) useUnmountSignal() — A React Hook to cancel promises when a component is unmounted. Retrieved from https://www.scien.cx/2021/07/27/useunmountsignal-a-react-hook-to-cancel-promises-when-a-component-is-unmounted/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.