j-labs
j-labs.pl › home › tech blog › how to use the useeffect hook with the abortcontroller
AbortController in React. How to use the useEffect hook with the AbortControl? | j‑labs
December 9, 2025 - In the example above, we have created an instance of AbortController and obtained the associated AbortSignal within the useEffect hook. We have initiated the asynchronous operation, fetchData(), passing the signal as an argument. If the component unmounts or if a dependency changes, the cleanup function returned by useEffect is triggered.
Reddit
reddit.com › r/reactjs › abortcontroller usage
r/reactjs on Reddit: AbortController usage
October 2, 2022 -
Was watching a video about All useEffect Mistakes Every Junior React Developer Makes and it mentions using AbortController (@17:20) and cleanup methods in useEffect to cancel api calls. So I search react docs and there is no mention of AbortController. The useEffect and cancel api call scenario in the video is not even covered in the react docs. Looking at a few open source projects I don't see the AbortController used anywhere.
So what is the convention when it comes to useEffect, api calls and cleanup?
Top answer 1 of 3
4
It depends as always. But if you have some expensive calls to your server the AbortController can really help to minimize server load. Why should you let an stale request run? Also what if your request would update state with stale data because your user is clicking through some filter options or something? I would say AbortController is perfect for this type of scenarios.
2 of 3
1
I'm not sure but the way I used it, was placing it into a ref and then calling abort() in a use Effect return function to cancel an async fetch (AbortController is supported by axios) that was supposed to update the component when it did finished (my issue was that sometimes if the user clicked too fast and caused the component to unmount before the fetch did resolve, the effect would attempt to update the state from the unmounted component.
Videos
14:25
I Cannot Believe Abort Controller Can Do This - YouTube
Abort Controller is criminally underrated (every react dev ...
06:37
Abort Fetch API Requests using AbortController - YouTube
06:50
Full React Tutorial #24 - useEffect Cleanup - YouTube
11:21
Cancelling a Fetch request in a useEffect hook - YouTube
12:15
Cancelling an Axios request in a useEffect hook - YouTube
Medium
eminfurkan.medium.com › managing-asynchronous-operations-with-abortcontroller-in-react-e9bec3565ec8
Managing Asynchronous Operations with AbortController in React | by Furkan Tezeren | Medium
July 30, 2023 - We also clean up by canceling the request using abortController.abort() when the component unmounts. ... import { useEffect, useState } from 'react'; function MyComponent() { const [data, setData] = useState(null); const controller = new AbortController(); const signal = controller.signal; useEffect(() => { async function fetchData() { try { const response = await fetch('https://my-api.com/endpoint', { signal }); const json = await response.json(); setData(json); } catch (error) { if (error.name === 'AbortError') { console.log('Fetching data was aborted'); } else { console.error(error); } } } fetchData(); return () => { controller.abort(); }; }, []); return ( <div> {data ?
Close
making.close.com › posts › introducting-use-abortable-effect-react-hook
Introducing useAbortableEffect: a simple React hook for running abortable effects | The Making of Close
June 18, 2020 - // Regular React effect hook. useEffect(() => { // do something return () => { /* cleanup */ }; }, [deps]); // An abortable effect hook. const abortControllerRef = useAbortableEffect( (abortSignal) => { // do something return (abortController) => { /* do cleanup, you should probably abort */ ...
Academind
academind.com › tutorials › useeffect-abort-http-requests
useEffect(), Http Requests & Aborting
July 9, 2020 - We can change our code that uses the isActive variable, to use AbortController by implementing the above mentioned steps: useEffect(() => { const abortCtrl = new AbortController(); const opts = { signal: abortCtrl.signal }; fetch('https://jsonplaceholder.typicode.com/todos/1', opts) .then((response) => response.json()) .then((data) => setTodo(data)) .catch((error) => console.log(error.message)); return () => abortCtrl.abort(); }, []); When any request is cancelled using the AbortController object, an AbortError is thrown.
DEV Community
dev.to › pallymore › clean-up-async-requests-in-useeffect-hooks-90h
Clean Up Async Requests in `useEffect` Hooks - DEV Community
October 12, 2019 - When is if(!isCancelled) inside the useEffect is being checked? ... Sr. FE Dev @ AWS - comments/blogs are my own. ... Sr. FE Dev @ AWS ... In your case using the flag is not going to work. If you are using fetch to make requests you can use AbortController (just provide the signal to the thunk action)
DEV Community
dev.to › bil › using-abortcontroller-with-react-hooks-and-typescript-to-cancel-window-fetch-requests-1md4
Using AbortController (with React Hooks and TypeScript) to cancel window.fetch requests - DEV Community
March 27, 2019 - // src/hooks/useProfileInformation.jsx import {useState, useEffect} from 'react' export function useProfileInformation({accessToken}) { const [profileInfo, setProfileInfo] = useState(null) useEffect(() => { const abortController = new AbortController() window .fetch('https://api.example.com/v1/me', { headers: {Authorization: `Bearer ${accessToken}`}, method: 'GET', mode: 'cors', signal: abortController.signal, }) .then(res => res.json()) .then(res => setProfileInfo(res.profileInfo)) return function cancel() { abortController.abort() } }, [accessToken]) return profileInfo } // src/app.jsx impor
Wanago
wanago.io › home › using abortcontroller to deal with race conditions in react
Using AbortController to deal with race conditions in React
April 11, 2022 - If we do that, abortController.abort() aborts multiple requests. The above also fixes the issue we’ve had with the race conditions. The user opens /posts/1 to see the first post, we start fetching the post with id 1, Not waiting for the first post, the user changes the page to /posts/2, the useEffect cleans after the previous post and runs abortController.abort(), we start fetching the post with id 2, the post loads without issues and is available almost immediately, The first post never finishes loading because we’ve already aborted the first request.
LogRocket
blog.logrocket.com › home › understanding react’s useeffect cleanup function
Understanding React’s useEffect cleanup function - LogRocket Blog
December 16, 2024 - This associates the controller and signal with the fetch request and lets us cancel it anytime using AbortController.abort(): useEffect(() => { const controller = new AbortController(); const signal = controller.signal; fetch(API, { signal: signal, }) .then((response) => response.json()) .then((response) => { // handle success }); return () => { // cancel the request before component unmounts controller.abort(); }; }, []);
GitHub
github.com › closeio › use-abortable-effect
GitHub - closeio/use-abortable-effect: Super simple React hook for running abortable effects based on the AbortController API.
useEffect(() => { // do something return () => { /* cleanup */ }; }, [deps]); const abortControllerRef = useAbortableEffect( (abortSignal) => { // do something return (abortController) => { /* do cleanup, you should probably abort */ }; }, [deps], );
Author closeio
DEV Community
dev.to › leapcell › do-you-really-know-abortcontroller-3628
Do You Really Know AbortController? - DEV Community
January 16, 2025 - For older browsers, consider adding a polyfill to support AbortController. In React, effects can inadvertently run in parallel if the component updates before a previous asynchronous task completes: function FooComponent({ something }) { useEffect(async () => { const data = await fetch(url + something); // Handle the data }, [something]); return <>...</>; } To avoid such issues, use AbortController to cancel previous tasks: function FooComponent({ something }) { useEffect(() => { const controller = new AbortController(); const { signal } = controller; (async () => { const data = await fetch(ur
LogRocket
blog.logrocket.com › home › the complete guide to the abortcontroller api
The complete guide to the AbortController API - LogRocket Blog
March 12, 2025 - Using AbortController, it looks like this: useEffect(() => { const controller = new AbortController(); const { signal } = controller; // Define all your handler functions const handleMouseMove = (e) => { /* update state */ }; const handleKeyPress = (e) => { /* update state */ }; const handleScroll = () => { /* update state */ }; const handleResize = () => { /* update state */ }; // Add all the listeners with the signal document.addEventListener('mousemove', handleMouseMove, { signal }); document.addEventListener('keydown', handleKeyPress, { signal }); window.addEventListener('scroll', handleScroll, { signal }); window.addEventListener('resize', handleResize, { signal }); // Just one line for cleanup!
Whitespectre
whitespectre.com › ideas › abortcontroller-api-javascript
AbortController API: A Modern Approach to JavaScript Lifecycle Events | Whitespectre
April 19, 2023 - Now let’s see the same example using AbortController: const videoContainerRef = useRef<HTMLDivElement>(null); const [loadVideo, setLoadVideo] = useState(false); useEffect(() => { const controller = new AbortController(); function onScroll(e) { const rect = videoContainerRef.current?.getBoundingClientRect(); if (rect && rect.top < window.innerHeight) { setLoadVideo(true); controller.abort(); } } window.addEventListener('scroll', onScroll, { signal: controller.signal }); return () => controller.abort(); }, []); While at first glance there’s not much of a difference between these two approaches, once we start adding up event handlers, it becomes much easier to just use AbortController, saving us from declaring a function dedicated to removing listeners.
Web Dev Simplified
blog.webdevsimplified.com › 2025-06 › advanced-abort-controller
These Advanced AbortController Features Are Amazing
June 23, 2025 - If you pass the signal property with the AbortController’s signal, then when you call controller.abort(), it will automatically remove the event listener as if you called removeEventListener. This is especially useful in frameworks like React where you want to clean up event listeners from useEffect, or when you have many event listeners you want to remove at the same time.
GitHub
github.com › facebook › react › issues › 25284
Bug: React.StrictMode causes AbortController to cancel · Issue #25284 · facebook/react
September 16, 2022 - const useAbortController = (abortControllerProp, shouldAutoRestart = false) => { const abortController = useRef(abortControllerProp || initAbortController()); useEffect(() => { if (shouldAutoRestart && abortController.current.signal.aborted) { abortController.current = initAbortController(); } }, [abortController.current.signal.aborted, shouldAutoRestart]); useEffect(() => () => abortController.current.abort(), []); return abortController.current; }; The "echoed" rendering of the component causes the the controller to go from aborted false -> true.
Author EdmundsEcho
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › AbortController
AbortController - Web APIs | MDN
September 17, 2025 - The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.