Open In App

How to add Timer in Next.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how we can add Timer in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components conditionally.

Approach: To add our Timer we are going to use the react-countdown-circle-timer package. The react-countdown-circle-timer package helps us to integrate different types of timers. So first, we will install the react-countdown-circle-timer package, and then we will add timer on our homepage.

Create NextJS Application: You can create a new NextJs project using the below command:

npx create-next-app gfg

Install the required package: Now we will install the react-countdown-circle-timer package using the below command:

npm i react-countdown-circle-timer

Project Structure: It will look like this.

Adding the Timer: After installing the react-countdown-circle-timer package we can easily add Timer in our app . For this example, we are going to add timer to our homepage.

Add the below content in the index.js file:

Javascript




import { CountdownCircleTimer } from 
    'react-countdown-circle-timer'
  
export default function Timer(){
  return (
    <div>
      <h3>GeeksforGeeks - Timer</h3>
      <CountdownCircleTimer
        isPlaying
        duration={10}
        colors={[
          ['#004777', 0.33],
          ['#F7B801', 0.33],
          ['#A30000', 0.33],
        ]}
      >
        {({ remainingTime }) => remainingTime}
      </CountdownCircleTimer>
    </div>
    
  )
}


Explanation: In the above example first, we are importing our CountdownCircleTimer component from the installed package. After that, we are using the component and set the initial properties like isPlaying, duration, and color. Then we are displaying the remainingTime.

Steps to run the application: Run the below command in the terminal to run the app.

npm run dev

Output:


Last Updated : 05 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads