kandi background
Explore Kits

Display Stopwatch in ReactJs

by vsasikalabe Updated: Jan 24, 2023

The Popular JavaScript library React creates user interfaces (UI). It produces reusable user interface elements that may be used to construct complex and interactive online applications. Because react employs a declarative programming paradigm, you may specify the intended user interface and let it handle refreshing the screen as necessary when the underlying data changes.  

A stopwatch is a clock made to count the seconds between activation and deactivation. We may build a stopwatch in ReactJS with the following method: We'll be able to start and pause our stopwatch. The stopwatch will run using the componentDidMount() lifecycle function. At each stage of a component's lifecycle, React's built-in lifecycle functions are called. You can use the methods as hooks to execute code at significant points in the life cycle. As a result, you can decide how a component mounts, updates, and unmounts.

 Here is an example of how you might implement this: 

Preview of the output that you will get on running this code from your IDE.


In this solution we use the React library.

JavaScriptLines of Code : 74License : CC BY-SA 4.0

class Timer extends React.Component {
  constructor(props) {

    this.state = {
      count: 0,
      pause: true,
      rotation: 0

  componentDidMount() {

  countTimers = () => {
    let counter = setInterval(() => {
      if (!this.state.pause) {
          count: this.state.count + 1,
          rotation: this.state.rotation === 359.9 ? 0 : 359.9,
    }, 1000);

  startHandler = () => {
      pause: false

  pauseHandler = () => {
      pause: true

  render() {
    const {rotation, count} = this.state;
    let days = Math.floor(count / (1 * 60 * 60 * 24));
    let hours = Math.floor((count % (1 * 60 * 60 * 24)) / (1 * 60 * 60));
    let minutes = Math.floor((count % (1 * 60 * 60)) / (1 * 60));
    let seconds = Math.floor((count % (1 * 60)) / 1); 

    return (
      <div className="Timer">
          <div className="stopwatch-wrapper">
              <span className="days">{days}:</span>
              <span className="hours">{hours}:</span>
              <span className="minutes">{minutes}:</span>
              <span className="seconds" style={{transform: `rotate(${rotation}deg)`}}>{seconds}</span>
          <div className="buttons-wrapper">
              <button id="start" onClick={this.startHandler}>START</button>
              <button id="pause" onClick={this.pauseHandler}>PAUSE</button>

ReactDOM.render( <
  Timer / > ,
  display: inline-block;
  transition: transform 500ms;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>


Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Copy the code using "copy" button above and paste it in App.js file,Style code into App.css and html code into index.html within the body tag.(remove the earlier code from App.js and App.css).
  6. import React library.
  7. Open the terminal from IDE.
  8. npm start to run the file.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.

I found this code snippet by searching for 'Display Stopwatch in ReactJs' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.

Using this solution, we are able to do display Stopwatch in ReactJs with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to do display Stopwatch in ReactJs.

Dependent Libraries

create-react-appby facebook

JavaScript star image 98671 Version:v5.0.1

License: Permissive (MIT)

Set up a modern web app by running one command.


create-react-appby facebook

JavaScript star image 98671 Version:v5.0.1 License: Permissive (MIT)

Set up a modern web app by running one command.

You can search for any dependent library on kandi like ' react '.


  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page

See similar Kits and Libraries