const func = () => {
const id = setTimeout(() => {
//do something with an element
}, 300);
return id;
};
useEffect(() => {
const id = func();
return () => clearTimeout(id);
}, [click]);
When I click too fast, which is the trigger to the useEffect, it could cause the function passed to setTimeout to not be runned.
What is the problem?
Is the following alternative solution just as good?
const func = () => {
const id = setTimeout(() => {
if (element is stilled defined) {
// do something
}
}, 300);
return id;
};
useEffect(() => {
func();
}, [click]);