Following is the doc I am using to learn React Custom Hooks.
Doc Link - https://react.dev/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks
In that page, there is a piece of code -
function useData(url) {
const [data, setData] = useState(null);
useEffect(() => {
if (url) {
let ignore = false;
fetch(url)
.then(response => response.json())
.then(json => {
if (!ignore) {
setData(json);
}
});
return () => {
ignore = true;
};
}
}, [url]);
return data;
}
I am not able to understand the use of ignore variable in this case. Why do we need to set ignore = true; in the cleanup phase.
ignoreis a bit weird, I tend to usemounted = true, and thenif (!mounted) {.....etc, it seems more logical.