Display Stopwatch in ReactJs

share link

by vsasikalabe dot icon Updated: Jan 24, 2023

technology logo
technology logo

Solution Kit Solution Kit  

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.


Code:

In this solution we use the React library.

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

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

  componentDidMount() {
    this.countTimers();
  }

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

  startHandler = () => {
    this.setState({
      pause: false
    })
  }

  pauseHandler = () => {
    this.setState({
      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">
          <h1>{'STOPWATCH'}</h1>
          <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>
          <div className="buttons-wrapper">
              <button id="start" onClick={this.startHandler}>START</button>
              <button id="pause" onClick={this.pauseHandler}>PAUSE</button>
          </div>
      </div>
    );
  }
}

ReactDOM.render( <
  Timer / > ,
  document.getElementById("react")
);
.seconds{
  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>

Instructions

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 doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.

Support
    Quality
      Security
        License
          Reuse

            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.
            Support
              Quality
                Security
                  License
                    Reuse

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

                      Support

                      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