How to create an Analog clock using React
Learn to make a wall clock with react js

Search for a command to run...
Learn to make a wall clock with react js

Thank you.
Authentication is a fundamental aspect of modern web and mobile applications. It ensures that users can securely access an app while protecting their data. Firebase, a platform developed by Google, offers a simple and efficient way to add authenticat...

Hello there! My name is Shivam, and today, I will show you how to create this beautiful horizontal scrolling parallax effect with HTML, CSS, and JavaScript. Wouldn't this look stunning on your blog or portfolio site? To achieve this effect, we will ...

Dealing with fonts can get quite overwhelming. For quite some time, I was searching for a way to self-host google fonts because the google fonts API's network request increased the render-blocking time more than I expected. I stumbled upon fontsource...

Our favorite month is here! And we have some amazing activities planned for our community 🤩 Yes, you are right. We are talking about Hacktoberfest! In this blog, we want to cover various ways you can participate in Hacktoberfest through the ReactPla...

The second edition of ReactPlay Hackathon is here!

Analog clock is one of the many plays that I developed at ReactPlay to get my hands dirty in the world of open source. It is just a digital representation of an old-styled wall clock made with reactjs.
In this play, you will get to learn how to work with Date and time, how to synchronize the time, how to extract the hour, minute, and seconds info, and use this info to get the correct angle to move the clock hands toward the right direction. In the end, you will be able to make a beautiful wall clock.
ReactPlay is an open-source platform to create, collaborate and showcase tiny bite-sized projects called plays. It is an excellent opportunity to showcase, get feedback, and collaborate with fellow developers to get a real-world experience.

TLDR; Learn to make a beautiful wall clock in reactjs. Live Demo Github
Well, It's not a revolutionary idea. I saw it somewhere on the internet and I was inspired by the idea that I could also make something similar. The static clock interface wasn't difficult but the challenge I faced was how do I make those clock hands move? with a little bit of googling, I was convinced that I could make use of css transform:rotate() property to rotate those hands. I just need to get the correct angles to rotate those moves.
Since this is a react project we will be focusing on interactivity and not design. However, feel free to check out the github repo
To begin with, we have a circular container called a clock and inside this circular container, we have three divs designed as hour-hand, minute-hand, and second-hand. Pretty simple, isn't it?
<div className="clock">
<div className="hour-hand" ></div>
<div className="minute-hand"></div>
<div className="second-hand"></div>
</div>
Alright, I have the design but it doesn't do anything.
So, the next goal is to get the time and keep it synchronized each second.
In the code snippet below, We are using javascript Date object to get the time. setInterval function to update the time every second, and clearInterval to clear this interval, or else this will keep running in the background even if the user goes to some other plays. which may affect the performance.
const [date, setDate] = useState(new Date());
useEffect(() => {
const interval = setInterval(() => {
setDate(new Date());
}, 1000);
return () => clearInterval(interval);
}, []);
new Date() object give us date and timeMon Jun 27 2022 08:36:32 GMT+0530 (India Standard Time) {} we don't need the date hence we need to just extract the time. getHours(), getMinutes(), and getSeconds() are js methods to get the hours, minutes and seconds respectively out of date object.
In order to complete a circle round each hand has to move 360 degrees. for ex.,
const hour = date.getHours() * 30 // Move 30degree on first hour, 60 degree on second and so on.
const minute = date.getMinutes() * 6 //same as above.
const second = date.getSeconds() * 6 //same as above.
Since we, now have the angles of each hand. We, just need to incorporate the angles into the design.
<div className="clock">
<div className="hour-hand"
style={{ transform: "rotate(" + hour + "deg)" }}
></div>
<div className="minute-hand"
style={{ transform: "rotate(" + minute + "deg)" }}
></div>
<div className="second-hand"
style={{ transform: "rotate(" + second + "deg)"}}
></div>
</div>
It was an awesome experience to create and share this play with you. I thank you for stopping by. Please feel free to reach out on twitter or leave your constructive feedback in the comment section.