Create a Stop Watch using ReactJS
Last Updated :
24 May, 2023
We can create Stop Watch in ReactJS using the following approach. Our StopWatch will have the functionality of Start, Pause, Resume and Reset.
Creating React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app stopwatch
Step 2: After creating your project folder i.e. stopwatch, move to it using the following command.
cd stopwatch
Create a Components folder insider the src folder. Inside the Components folder create three different subfolders with the names StopWatch, Timer, ControlButtons. Now make a .jsx and a .css for each components.
Project Structure: It will look like the following.

Components used in our applications are:

Example: The outer component is StopWatch, the blue marked is the Timer, and the green-colored component will be denoted as ControlButtons.
index.js
JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
App.js
JavaScript
import './App.css';
import StopWatch from './Components/StopWatch/StopWatch.js';
function App() {
return (
<div className="App">
<StopWatch />
</div>
);
}
export default App;
App.css
CSS
.App {
background-color: rgb(238, 238, 238);
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
StopWatch.jsx
JavaScript
import React, { useState } from "react";
import "./StopWatch.css";
import Timer from "../Timer/Timer";
import ControlButtons from "../ControlButtons/ControlButtons";
function StopWatch() {
const [isActive, setIsActive] = useState(false);
const [isPaused, setIsPaused] = useState(true);
const [time, setTime] = useState(0);
React.useEffect(() => {
let interval = null;
if (isActive && isPaused === false) {
interval = setInterval(() => {
setTime((time) => time + 10);
}, 10);
} else {
clearInterval(interval);
}
return () => {
clearInterval(interval);
};
}, [isActive, isPaused]);
const handleStart = () => {
setIsActive(true);
setIsPaused(false);
};
const handlePauseResume = () => {
setIsPaused(!isPaused);
};
const handleReset = () => {
setIsActive(false);
setTime(0);
};
return (
<div className="stop-watch">
<Timer time={time} />
<ControlButtons
active={isActive}
isPaused={isPaused}
handleStart={handleStart}
handlePauseResume={handlePauseResume}
handleReset={handleReset}
/>
</div>
);
}
export default StopWatch;
StopWatch.css
CSS
.stop-watch {
height: 85vh;
width: 23vw;
background-color: #0d0c1b;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
Three states used in the StopWatch component.
- time: It stores the time elapsed since you pressed start.
- isActive: It tells if the stop watch is in active state (i.e it is running, or it is being paused).
- isPaused: It tells if the stop watch is in active state and is paused or not.
Timer.jsx
JavaScript
import React from "react";
import "./Timer.css";
export default function Timer(props) {
return (
<div className="timer">
<span className="digits">
{("0" + Math.floor((props.time / 60000) % 60)).slice(-2)}:
</span>
<span className="digits">
{("0" + Math.floor((props.time / 1000) % 60)).slice(-2)}.
</span>
<span className="digits mili-sec">
{("0" + ((props.time / 10) % 100)).slice(-2)}
</span>
</div>
);
}
Timer.css
CSS
.timer {
margin: 3rem 0;
width: 100%;
display: flex;
height: 12%;
justify-content: center;
align-items: center;
}
.digits {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 3rem;
color: #f5f5f5;
}
.mili-sec {
color: #e42a2a;
}
ControlButtons.jsx
JavaScript
import React from "react";
import "./ControlButtons.css";
export default function ControlButtons(props) {
const StartButton = (
<div className="btn btn-one btn-start"
onClick={props.handleStart}>
Start
</div>
);
const ActiveButtons = (
<div className="btn-grp">
<div className="btn btn-two"
onClick={props.handleReset}>
Reset
</div>
<div className="btn btn-one"
onClick={props.handlePauseResume}>
{props.isPaused ? "Resume" : "Pause"}
</div>
</div>
);
return (
<div className="Control-Buttons">
<div>{props.active ? ActiveButtons : StartButton}</div>
</div>
);
}
ContolButtons.css
CSS
<pre>.Control-Buttons {
margin: 3rem 0;
width: 100%;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.btn-grp {
display: flex;
align-items: center;
justify-content: space-around;
}
.btn {
font-family: Verdana, Geneva, Tahoma, sans-serif;
width: 10vw;
height: 5vh;
border-radius: 14px;
margin: 0px 6px;
display: flex;
border: 2px solid #e42a2a;
justify-content: center;
align-items: center;
cursor: pointer;
color: #f5f5f5;
}
.btn-one {
background-color: #e42a2a;
}
ControlButtons Rendering: If user haven't started the stop watch then you are supposed to show only the start button. If the user have started the stop watch then you are supposed to show the reset and resume/pause buttons.
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.

Similar Reads
BMI Calculator Using React
In this article, we will create a BMI Calculator application using the ReactJS framework. A BMI calculator determines the relationship between a person's height and weight. It provides a numerical value that categorizes the individual as underweight, normal weight, overweight, or obese.Output Previe
3 min read
Create Rock Paper Scissor Game using ReactJS
In this article, we will create Rock, Paper, Scissors game using ReactJS. This project basically implements class components and manages the state accordingly. The player uses a particular option from Rock, Paper, or Scissors and then Computer chooses an option randomly. The logic of scoring and win
6 min read
Create a Form using React JS
Creating a From in React includes the use of JSX elements to build interactive interfaces for user inputs. We will be using HTML elements to create different input fields and functional component with useState to manage states and handle inputs. Prerequisites:Functional ComponentsJavaScript ES6JSXPr
5 min read
Create a Random Joke using React app through API
In this tutorial, we'll make a website that fetches data (joke) from an external API and displays it on the screen. We'll be using React completely to base this website. Each time we reload the page and click the button, a new joke fetched and rendered on the screen by React. As we are using React f
3 min read
Nutrition Meter - Calories Tracker App using React
GeeksforGeeks Nutrition Meter application allows users to input the name of a food item or dish they have consumed, along with details on proteins, calories, fat, carbs, etc. Users can then keep track of their calorie intake and receive a warning message if their calorie limit is exceeded. The logic
9 min read
Currency converter app using ReactJS
In this article, we will be building a very simple currency converter app with the help of an API. Our app contains three sections, one for taking the user input and storing it inside a state variable, a menu where users can change the units of conversion, and finally, a display section where we dis
4 min read
Lap Memory Stopwatch using React
Stopwatch is an application which helps to track time in hours, minutes, seconds, and milliseconds. This application implements all the basic operations of a stopwatch such as start, pause and reset button. It has an additional feature using which we can keep a record of laps which is useful when we
5 min read
Typing Speed Tester using React
In this article, we will create a Typing Speed Tester that provides a random paragraph for the user to type as accurately and quickly as possible within a fixed time limit of one minute. This application also displays the time remaining, counts mistakes calculates the words per minute and characters
9 min read
Number Format Converter using React
In this article, we will create Number Format Converter, that provides various features for users like to conversion between decimal, binary, octal and hexadecimal representations. Using functional components and state management, this program enables users to input a number and perform a range of c
7 min read
Create a Password Validator using ReactJS
Password must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in ReactJS. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions a
2 min read