Implementation of Stopwatch
Step-by-Step Implementation of the Project
In this lesson, we will get our hands dirty with the actual implementation of a stopwatch application in React JS. Let’s get started with the initial project setup!
Set up the environment
As the first step to get started with the implementation, we need to set up the environment for the project. Follow the below steps to set up the project.
- Open VScode
- Create a new folder named ‘stopwatch’
- Open ‘new terminal’
- Enter the command ‘npx create-react-app app’. This will create a new React JS project with the name ‘app’.
- Once the installation is complete, enter the command ‘cd app & npm start’ to start the React JS application in localhost:3000.
After removing the unnecessary files and folders, the app directory will look like this.
Create Application Structure
In this step, we will create the basic structure to the application such as container, buttons and display area. This will make it easy to view the elements we need for the application.
- For the stopwatch, we need one display area to run the timer and three button controls such as start, stop and reset.
- As the first stage, create a components folder in the src directory and create a file with the name ‘Stopwatch.jsx’
- Now, copy and paste the code to create the basic structure of the application.
// Stopwatch.jsx
// Functional Component to Create the Stopwatch
function Stopwatch() {
return (
// The main container
<div>
// The Display container
<div>
<h1>00:00:00</h1>
// The controls container
<div>
// The Start button
<button>
Start
</button>
// The Stop button
<button>
Stop
</button>
// The Reset button
<button>
Reset
</button>
</div>
</div>
</div>
);
}
export default Stopwatch; // Export the Stopwatch component - After the stopwatch component export, you need to import the component in the main component ‘App.jsx’ since it is the entry point for your application.
- Modify the code in ‘App.jsx’ file similar to the below code.
// App.jsx
// Import the Stopwatch component
import Stopwatch from "./components/Stopwatch";
function App() {
return (
// The main container
<div>
// Application Title
<h1>Stopwatch</h1>
// Use Stopwatch component
<Stopwatch />
<a href="https://www.guvi.in/">
Made by Guvi💚
</a>
</div>
);
}
export default App;- After updating the App component, you will see the following result.
Apply Styles to the Application
The next step is to add styles to each element to make it visually appealing using CSS. We can create n styling files one for each component. In this project, we will create two css files, one for the stopwatch component and another for the App component.
- Modify the Stopwatch component to add styling for each element. Using the ‘className’ attribute, we can match the styling to the element. Make sure to import the style file into the component.
// Stopwatch.jsx
import "./Stopwatch.css";
function Stopwatch() {
return (
<div className="stopwatch-container">
<div className="display-container">
{formatTime()}
<div className="controls">
<button className="start-btn" onClick={start}>
Start
</button>
<button className="stop-btn" onClick={stop}>
Stop
</button>
<button className="reset-btn" onClick={reset}>
Reset
</button>
</div>
</div>
</div>
);
}
export default Stopwatch;- The next step is to create a styling file for stopwatch. Create a file in the component folder ‘Stopwatch.css’. And write styles for each element.
/* Stopwatch.css */
/* Main body styling */
body{
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: hsl(0, 6%, 90%);
}
/* Main container for stopwatch component */
.stopwatch-container{
display: flex;
flex-direction: column;
align-items: center;
}
/* Display area */
.display-container{
border: 5px solid #000;
border-radius: 50px;
background-color: #fff;
padding: 30px;
display: flex;
flex-direction: column;
align-items: center;
font-size: 3.5rem;
gap: 2.5rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
color: rgb(74, 71, 71);
margin: 2rem 2.5rem;
}
/* General styling for buttons */
button{
padding: 1rem 2rem;
font-size: 1rem;
font-weight: 550;
border: 3px solid #000;
border-radius: 50px;
margin: 0 1.5rem;
cursor: pointer;
min-width: 120px;
transition: background-color 0.3s ease;
color: #fff;
}
/* General positioning of controls */
.controls{
display: flex;
justify-content: space-between;
gap: 2rem;
}
/* Start button */
.start-btn{
background-color: green;
}
.start-btn:hover{
background-color: hsl(120, 100%, 50%);
}
/* Stop button */
.stop-btn{
background-color: rgb(167, 7, 7);
}
.stop-btn:hover{
background-color: hsl(0, 100%, 56%);
}
/* Reset button */
.reset-btn{
background-color: rgb(6, 22, 197);
}
.reset-btn:hover{
background-color: hsl(205, 100%, 50%);
}- Now, let’s add some styling to the App component.
// App.js
import "./App.css";
import Stopwatch from "./components/Stopwatch";
function App() {
return (
<div className="container">
<h1 className="title">Stopwatch</h1>
<Stopwatch />
<a href="https://www.guvi.in/" className="copywright">
Made by Guvi💚
</a>
</div>
);
}
export default App;- Modify the App.css file to include new stylings.
/* App.css */
.container{
display: flex;
flex-direction: column;
align-items: center;
}
.title{
font-size: 3rem;
}
.copywright{
color: #333;
text-decoration: none;
}- Once you add the styling, the application will look like the below output.
Add Logic to the Application
The last part of the application is to add the logic for each button and connect the controls to the display. To do that, you do not need to create a separate file. We can achieve the logic by using useEffect(), useState() and useRef() functions we saw in the previous module.
- Add the logic to the stopwatch component.
// Stopwatch.jsx
// Import the functions from the react package
import React, { useState, useEffect, useRef } from "react";
function Stopwatch() {
// Define the variables
const [isRunning, setIsRunning] = useState(false);
const [elapsedTime, setElapsedTime] = useState(0);
const startTimeRef = useRef(0);
const intervalIdRef = useRef(null);
// To set the elapsed time according to controls (start or stop)
useEffect(() => {
if(isRunning){
intervalIdRef.current = setInterval(() =>{
setElapsedTime(Date.now() - startTimeRef.current);
}, 10); // It occurs every 10 ms if there is any change in the run state
}
// Clear the display
return () =>{
clearInterval(intervalIdRef.current);
}
}, [isRunning]);
// Logic behind start button
function start() {
setIsRunning(true);
startTimeRef.current = Date.now() - elapsedTime;
}
// Logic behind stop button
function stop() {
setIsRunning(false);
}
// Logic behind reset button
function reset() {
setIsRunning(false);
setElapsedTime(0);
}
// Logic behind display timer
function formatTime() {
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
let seconds = Math.floor((elapsedTime / (1000)) % 60);
let milliseconds = Math.floor((elapsedTime % 1000) / 10);
hours = String(hours).padStart(2, "0");
minutes = String(minutes).padStart(2, "0");
seconds = String(seconds).padStart(2, "0");
milliseconds = String(milliseconds).padStart(2, "0");
return `${hours} : ${minutes} : ${seconds} : ${milliseconds}`;
}
return (
<div className="stopwatch-container">
<div className="display-container">
{formatTime()}
<div className="controls">
<button className="start-btn" onClick={start}>
Start
</button>
<button className="stop-btn" onClick={stop}>
Stop
</button>
<button className="reset-btn" onClick={reset}>
Reset
</button>
</div>
</div>
</div>
);
}
export default Stopwatch;With adding the logic to the component, you will now be able to see the entire working of the controls and display. The output will look like this
Congratulations 🎉!
You have successfully created your own stopwatch application.
In the next lesson, we will look into the steps to deploy the project using vercel.
See you in the next lesson!


