Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to add Analog Clock in ReactJS ?

  • Last Updated : 14 Dec, 2021

In this article, we are going to learn how we can add Analog Clock in ReactJs. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies.

Approach: To add our Analog clock we are going to use the analog-clock-react package because it is powerful, lightweight, and fully customizable. After that, we will add our clock on our homepage using the installed package.

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

npx create-react-app gfg

Install the required package: Now we will install the analog-clock-react package using the below command:

npm i analog-clock-react

Project Structure: It will look like this.

Adding Clock: In this example, we are going to add the clock on the homepage of our app using the package that we installed. For this, add the below content in the App.js file.

App.js




import AnalogClock from 'analog-clock-react';
   
export default function ReactClock() {
  let options = {
    width: "300px",
    border: true,
    borderColor: "#2e2e2e",
    baseColor: "#17a2b8",
    centerColor: "#459cff",
    centerBorderColor: "#ffffff",
    handColors: {
      second: "#d81c7a",
      minute: "#ffffff",
      hour: "#ffffff"
    }
  };
   
  return (
    <div>
      <h2>React Clock - GeeksforGeeks</h2>
      <AnalogClock {...options} />
    </div>
  )
}

Explanation: In the above example first, we are importing the AnalogClock component from the analog-clock-react package. After that, we are creating an options variable and storing the settings in this variable. Then we are using our AnalogClock component to display the clock.

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

npm start

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!