Answer the question
In order to leave comments, you need to log in
How to clear a timer written through a promise when switching to another component in React?
const timer = new Promise(resolve => setTimeout(resolve, 5000));
Answer the question
In order to leave comments, you need to log in
class Hello extends React.Component {
componentWillUnmount() {
if (this.timer) {
clearTimeout(this.timer);
}
}
someMethod() {
return new Promise((resolve) => {
this.timer = setTimeout(resolve, 5000);
});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question