DEV Community


Posted on

React JS: Countdown timer restart when page is reloaded Simple solution

React <Countdown/> is a customizable countdown component for React.

It seems that we lost the counter if we reload the browser page. To avoid that we will use the local storage to persist our timer data.

We will use the package example source code

You can find the final solution here

But first, we verify that we have installed the required packages and have imported them. Our code header must look like this

import React from "react";
import ReactDOM from "react-dom";
import Countdown from "react-countdown";
import { useState, useEffect } from "react";
Enter fullscreen mode Exit fullscreen mode

Next is our counter renderer function component.

// Random component
const Completionist = () => <span>You are good to go!</span>;

// Renderer callback with condition
const renderer = ({ hours, minutes, seconds, completed }) => {
  if (completed) {
    // Render a complete state
    return <Completionist />;
  } else {
    // Render a countdown
    return (
Enter fullscreen mode Exit fullscreen mode

Our next step will be a function to read data from local storage

const getLocalStorageValue = (s) => localStorage.getItem(s);
Enter fullscreen mode Exit fullscreen mode

Next we create our App function component and defined required data. The most important data is the delay value.

const App = () => {
  const [data, setData] = useState(
    { date:, delay: 60000 } //60 seconds
  const wantedDelay = 60000; //60 s

  return (
        date={ + data.delay}
        onStart={(delta) => {
          //Save the end date
          if (localStorage.getItem("end_date") == null)
              JSON.stringify( + data.delay)
        onComplete={() => {
          if (localStorage.getItem("end_date") != null)
Enter fullscreen mode Exit fullscreen mode

Now our main problem is how to handle the browser page reloaded; for that we will use a react hook (with class component it will be componentDidMount function).

  //[START] componentDidMount
  //Code runs only one time after each reloading
  useEffect(() => {
    const savedDate = getLocalStorageValue("end_date");
    if (savedDate != null && !isNaN(savedDate)) {
      const currentTime =;
      const delta = parseInt(savedDate, 10) - currentTime;

      //Do you reach the end?
      if (delta > wantedDelay) {
        //Yes we clear uour saved end date
        if (localStorage.getItem("end_date").length > 0)
      } else {
        //No update the end date  
        setData({ date: currentTime, delay: delta });
  }, []);
  //[END] componentDidMount

Enter fullscreen mode Exit fullscreen mode

Instead of using localstorage, we can use a remote server or anything else. The most important is to save our end date of the count down.


Top comments (2)

johnmethew18 profile image
john methew

Thanks for sharing, the sample code i will tested to sort out local storage issue

aliberro42 profile image
Ali Berro🇵🇸

thank you so much great work!