Skip to content
Advertisement

How to stop ReactJS countdown timer from re-rendering?

I’m currently using a simple Countdown Timer component. This is the code of the component:

JavaScript

My issue is that everytime I refresh my page, the timer starts again. I understand this has something to do with react re-rendering the components and I need to move this to a higher level to save state, but I’m not sure exactly how to do that.

Also, I use the time as a sort of prop, where everytime a user creates a new listing on my website, a new timer starts for that particular listing.

So how exactly can I make my timer save state for already created listings, while at the same time allowing it to create new listings?

Thanks!

Advertisement

Answer

First of all, I’m not sure what do you mean by “refresh page”, if you mean that you hit ctrl+r and reload browser tab, the you restart your application entirely and it’s not about react state. In this case you need to store somewhere your current state and update your local state from that data after refresh.

So you need to save your timer start time in local storage or on server side so you would have an ability to initialize your timer using that time and it will continue running after page reload

Advertisement