Cancelling Request
AbortController
allows to cancel or abort asynchronous operation like fetch request or DOM manipulation
useEffect(() => {
const controller = new AbortController();
axios
.get<User[]>("https://jsonplaceholder.typicode.com/users", {
signal: controller.signal,
})
.then((res) => {
setUsers(res.data);
})
.catch((err) => {
if (err instanceof CanceledError) return;
setError(err.message);
});
return () => {
controller.abort();
};
}, []);