Open In App

Creating a Timer Component with useEffect Hook in React

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
32 Likes
Like
Report

Timer Component using React along with its useEffect hook will not only display the elapsed time since the component mounts but also allow users to set and manage the countdown timer dynamically. We will harness the power of the useEffect hook to manage side effects and update the timer seamlessly.

Output Preview: Let us have a look at how the final feature will look like.

preview
Output Preview

Prerequisites:

Functionalities of Timer Component:

  • Elapsed Time Display: Upon mounting, the Timer Component will display the elapsed time in hours, minutes, and seconds.
  • Countdown Timer: Users can set custom countdown durations according to their requirements.
  • Dynamic Timer Updates: Leveraging the useEffect hook, the Timer Component will update in real-time, reflecting changes in countdown duration and elapsed time.
  • Timer Controls: Users will have control over the timer through intuitive buttons, enabling them to start, pause, reset, and edit the countdown duration seamlessly.

Approach to create Timer Component:

  • Component Initialization: We initialize the Timer Component with state variables to manage hours, minutes, seconds, timer activation status, and editing mode.
  • Countdown Logic: Utilizing the useEffect hook, we implement the logic to decrement the timer values at regular intervals while the timer is active.
  • User Interaction Handling: We implement functions to handle user interactions such as starting, pausing, resetting, and editing the timer duration.
  • Styling for Enhanced User Experience: CSS styles are applied to create an aesthetically pleasing and intuitive user interface, enhancing the overall user experience.
  • Dynamic Updates: We ensure that the Timer Component dynamically updates in response to user actions, maintaining consistency and responsiveness throughout the countdown process.

Steps to Create the Project:

Step 1: Create a new React project using Create React App.

npx create-react-app timer-app

Step 2: Change the directory:

cd timer-app

Project Structure:

Screenshot-2024-02-21-231240
Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: Below is the code example of the Timer Component with useEffect.

JavaScript
// App.js

import React from 'react';
import './App.css';
import Timer from './Timer';

function App() {
    return (
        <div className="App">
            <header className="App-header">
                <h1>
                    Timer App
                </h1>
                <Timer />
            </header>
        </div>
    );
}

export default App;
JavaScript CSS CSS

Step to run the App:

npm start

Output: Open your web browser and go to http://localhost:3000 to view the Timer App.

screen-capture-ezgifcom-video-to-gif-converter
ReactJS Timer App Output



Next Article

Similar Reads