Answer the question
In order to leave comments, you need to log in
How to correctly send an asynchronous request to useEffect?
There is a component, a modal window. When you click outside the modal window, it closes, when you open it, an asynchronous request flies.
function handleClickOutside(e: MouseEvent) {
if (wrapModal.current) {
if (e.target && !wrapModal.current.contains(e.target as Node)) {
setIsOpen(false);
setCurrentImg(0);
setImg(null);
}
}
}
useEffect(() => {
const fetchData = async () => {
await axios.get(url).then((data: any) => {
setImg(data);
setPreloader(!preloader);
});
};
fetchData();
}, [url]);
useEffect(() => {
document.addEventListener("mousedown", handleClickOutside, false);
return () => {
document.removeEventListener("mousedown", handleClickOutside, false);
};
}, []);
Answer the question
In order to leave comments, you need to log in
In your case, you need to avoid updating the state if the component has been unmounted. The most correct, in my opinion, solution is to cancel the request when unmounting. fetch example, because I don't know if there is an option to cancel the request in axios.
useEffect(() => {
const abortController = new AbortController();
const fetchData = async () => {
await fetch(url, { abortController }).then((data: any) => {
setImg(data);
setPreloader(!preloader);
});
};
fetchData();
return () => {
abortController.abort();
}
}, [url]);
useEffect(() => {
let isMounted = true;
const fetchData = async () => {
...
if (isMounted) {
setImg(data);
setPreloader(!preloader);
}
...
}
...
return () => { isMounted = false; };
}, [])
code that allows you to check if the component is mounted in the DOM before doing setState
const isMounted = useIsMounted();
useEffect(() => {
...
if (isMounted()) {
setImg(data);
setPreloader(!preloader);
}
...
}, [])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question