Is it necessary for React to use setTimeout to clear timers and why?

is it necessary for React to use setTimeout to clear timers and why?

Mar.22,2021

Yes, all single-page applications should be cleared actively.
Why? Because it is a single-page application, the timer is stored in memory after running, even if the route is switched, it still does not leave on this page, so it will continue to run.
Why don't traditional pages need to be cleared? Because the traditional page is multi-page, clicking on a link is not a jump route, but opens a new page, and the data in the memory of the old page is automatically erased.


whether timer construction and destruction is recommended or clearly affects performance


Yes, otherwise, after you leave the page, if the timer is triggered and you read parameters such as this, you will directly undefined error


.

is not a performance issue, but the key point is that cannot guarantee that the callback function will not be called after the component is unloaded. Once this happens, the this will become undefined, and the code will report an error.

from this point of view, any asynchronous event should unsubscribe / listen when the component is unloaded, including pub/sub, dom events, etc.

Menu