Answer the question
In order to leave comments, you need to log in
How to properly clear one of the timeouts?
There is a code like this:
componentDidMount() {
this.foo = () = {
//some code
setTimeout(func, 1);
};
};
componentWillUnmount() {
setTimeout(func, 1);
//some code
this.foo();
}
componentWillUnmount() {
const timeout = setTimeout(func, 1);
//some code
clearTimeout(timeout);
this.foo();
}
Answer the question
In order to leave comments, you need to log in
The example is too synthetic.
componentWillUnmount as well as componentDidMount are guaranteed to be called once.
The this.foo function is called in componentWillUnmount without conditions. What's the point of separating implementation and execution? Tell me what problem you are trying to solve, then you won’t have to write crutches
componentDidMount() {
this.foo = (timerToClear) = {
//some code
if (timerToClear) {
clearTimeout(timerToClear);
}
setTimeout(func, 1);
};
};
componentWillUnmount() {
const timer = setTimeout(func, 1);
//some code
this.foo(timer);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question