Within my component, I have a timer that is initiated using setInterval
in the mounted()
hook.
Let's say this component is located at http://localhost:3000/some-route
.
What is the best approach to call clearInterval()
when navigating to another route such as http://localhost:3000/
in order to stop the timer?
I have attempted to use unmounted()
, but the component does not always unmount when changing routes. If I return to the same route (/some-route
), the setInterval
function restarts since the component remounts.
How can I ensure that the interval is cleared every time a different route is accessed?