This React JS component helps you to display an analog clock in your web/app project. It renders an SVG clock face with rotating hour, minute, and second hands. The clock class uses React’s component lifecycle methods to update the clock every second.
How to Display Analog Clock in React JS
First of all, load the Normalize CSS by adding the following CDN link into the head tag of your webpage.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
After that, create a div element with a unique id in which React JS render the clock widget.
<div id="root"> <!-- This div's content will be managed by React. --> </div>
Now, add the following CSS styles to your project for the clock face. You can customize the clock size, background, and box shadow by changing the CSS rules according to your needs.
@import url("https://fonts.googleapis.com/css?family=Roboto"); .clock-container { position: relative; width: 20%; overflow: hidden; background: #fff; margin-left: auto; margin-right: auto; display: block; } .clock-container.styling { box-shadow: 0px 0px 10px #888888; border-radius: 50%; min-width: 100px; margin-top: 1%; transform: scale(1.7); margin: 30px; } .clock-container:before { content: ""; display: block; padding-top: 100%; } .clock-content { position: absolute; top: 0; left: 0; bottom: 0; right: 0; display: flex; justify-content: center; align-items: center; } .clock-content .background-numbers { position: absolute; width: 90%; height: 90%; top: 5%; left: 5%; z-index: 1; } .clock-content .background-numbers .numbers { fill: grey; } .clock-content .indicator { border-radius: 10px; transform-origin: bottom; outline: 1px solid transparent; position: absolute; bottom: 50%; } .clock-content .indicator.seconds-indicator { width: 0.5%; height: 37.5%; z-index: 2; background-color: #dfc48c; } .clock-content .indicator.minutes-indicator { width: 1%; height: 30%; z-index: 3; background-color: #cd6a51; } .clock-content .indicator.hours-indicator { width: 1.5%; height: 22.5%; z-index: 4; background-color: #8f9c6c; } .clock-content .indicator-cover { position: absolute; bottom: 48.5%; /*(50% - (3% /2)) */ border-radius: 50%; width: 3%; height: 3%; background-color: lightgrey; z-index: 5; } .digital { margin-top: 50px; text-align: center; font-family: "Roboto", sans-serif; font-size: 20px; }
Load the React JS library by adding the following CDN links before closing the body tag:
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>
Finally, add the following JavaScript code to activate the clock component.
class Clock extends React.Component { constructor(props) { super(props); this.state = { date: new Date() }; } componentDidMount() { this.timerID = setInterval(() => this.tick(), 1000); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { const hoursDegrees = this.state.date.getHours() * 30 + this.state.date.getMinutes() / 2; const minutesDegrees = this.state.date.getMinutes() * 6 + this.state.date.getSeconds() / 10; const secondsDegrees = this.state.date.getSeconds() * 6; const divStyleHours = { transform: "rotateZ(" + hoursDegrees + "deg)" }; const divStyleMinutes = { transform: "rotateZ(" + minutesDegrees + "deg)" }; const divStyleSeconds = { transform: "rotateZ(" + secondsDegrees + "deg)" }; return /*#__PURE__*/( React.createElement("div", null, /*#__PURE__*/ React.createElement("div", { className: "clock-container styling" }, /*#__PURE__*/ React.createElement("div", { id: "clock", className: "clock-content" }, /*#__PURE__*/ React.createElement("svg", { className: "background-numbers", viewBox: "0 0 226.6 233.8" }, /*#__PURE__*/React.createElement("path", { className: "numbers", d: "M105.5 22.7V6.4h-5.9V4.3c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7zM114.1 4.8c.3-1 .8-1.8 1.4-2.5.6-.7 1.4-1.3 2.4-1.7.9-.4 2-.6 3.2-.6 1 0 1.9.1 2.8.4.9.3 1.6.7 2.3 1.2.6.5 1.1 1.2 1.5 2 .4.8.6 1.7.6 2.8 0 1-.2 1.9-.5 2.7s-.7 1.5-1.2 2.1c-.5.6-1.1 1.2-1.8 1.6-.7.5-1.3 1-2 1.4-.7.4-1.4.8-2.1 1.3s-1.3.9-1.9 1.3c-.6.5-1.1 1-1.5 1.5s-.7 1.2-.8 1.9h11.6v2.4h-14.8c.1-1.3.3-2.5.7-3.4.4-.9.8-1.8 1.4-2.5s1.2-1.3 2-1.9c.7-.5 1.5-1 2.3-1.5 1-.6 1.8-1.1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.7 2.4s-.8 1.4-1.3 2c-.6.6-1.2 1-2.1 1.4s-1.8.5-3 .5-2.2-.2-3-.5-1.5-.8-2.1-1.4c-.6-.6-1-1.2-1.3-2s-.6-1.6-.7-2.4c-.2-.9-.3-1.7-.3-2.6 0-.9-.1-1.8-.1-2.6.1-.8.1-1.7.1-2.6zm2.9 5.4c.1 1.1.2 2 .5 3 .3.9.8 1.7 1.4 2.4s1.5 1 2.7 1c1.2 0 2-.3 2.7-1s1.1-1.4 1.4-2.4c.3-.9.5-1.9.5-3 .1-1.1.1-2 .1-2.9V63c0-.7-.1-1.3-.2-2s-.2-1.3-.4-2c-.2-.6-.4-1.2-.8-1.7s-.8-.9-1.3-1.2-1.2-.4-2-.4-1.4.1-2 .4c-.5.3-1 .7-1.3 1.2-.4.5-.6 1-.8 1.7-.2.6-.3 1.3-.4 2-.1.7-.1 1.3-.2 2v1.8c.1.9.1 1.9.1 2.9z" }), /*#__PURE__*/React.createElement("g", null, /*#__PURE__*/React.createElement("path", { className: "numbers", d: "M53.5 38.2V21.9h-5.9v-2.2c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7zM69.1 38.2V21.9h-5.9v-2.2c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7z" }))), /*#__PURE__*/ React.createElement("div", { id: "hours-indicator", className: "indicator hours-indicator " + ( this.state.date.getHours() === 0 ? "" : "transition-effect"), style: divStyleHours }), /*#__PURE__*/ React.createElement("div", { id: "minutes-indicator", className: "indicator minutes-indicator " + ( this.state.date.getMinutes() === 0 ? "" : "transition-effect"), style: divStyleMinutes }), /*#__PURE__*/ React.createElement("div", { id: "seconds-indicator", className: "indicator seconds-indicator " + ( this.state.date.getSeconds() === 0 ? "" : "transition-effect"), style: divStyleSeconds }), /*#__PURE__*/ React.createElement("div", { className: "indicator-cover" }))))); }} ReactDOM.render( /*#__PURE__*/React.createElement(Clock, null), document.getElementById("root"));
That’s all! hopefully, you have successfully displayed an analog clock in React JS apps. If you have any questions or suggestions, feel free to comment below.