Skip to main content

Command Palette

Search for a command to run...

Building a Countdown Timer Using React.js

Updated
3 min read
Building a Countdown Timer Using React.js
D

Bio: 🖥️ Computer Science Student | ✍️ Passionate Blogger

💡 Exploring the Intersection of Tech and Creativity

🎓 Currently pursuing a degree in Computer Science, I am a curious and driven student with a deep passion for all things technology. I am constantly seeking new avenues to expand my knowledge and skills in this ever-evolving field.

📚 As an aspiring computer professional, I am immersed in the world of programming languages, algorithms, and software development. However, my true excitement lies in blending my technical expertise with my creative spirit.

✏️ I have recently embarked on an exciting journey as a blogger, where I channel my love for writing to explore the fascinating world of technology, digital trends, and innovative ideas. Through my blog, I aim to share valuable insights, tutorials, and thought-provoking content that inspires others to embrace the wonders of the digital age.

🌐 When I'm not busy coding or crafting blog posts, you can find me tinkering with gadgets, experimenting with new software, or exploring the latest tech innovations. I'm always on the lookout for fresh perspectives and innovative ideas to incorporate into my work.

🤝 Let's connect and explore the limitless possibilities of the tech realm together! Feel free to reach out if you have any questions, collaboration opportunities, or just want to geek out over the latest trends. Let's shape the future of technology one blog post at a time!

#ComputerScienceStudent #BloggingEnthusiast #TechGeek #CodeAndCreativity

In the previous blog, we explored the State and event handler. Now, In this tutorial, we will explore how to create a countdown timer using React.js. React.js is a popular JavaScript library for building user interfaces, and it provides a simple and efficient way to handle the state of our application.

Prerequisites

Before we start, make sure you have the following installed:

  • Node.js

  • npm (Node Package Manager)

Step 1: Setting Up the Project

  1. Open your terminal and create a new directory for your project:

     mkdir countdown-timer
     cd countdown-timer
    
  2. Initialize a new React.js project:

     npx create-react-app .
    

    This command will set up a new React.js project in the current directory.

  3. Start the development server:

     npm start
    

    This will start the development server, and you can see your React app running in your browser at http://localhost:3000.

Step 2: Creating the Countdown Component

  1. Open the src directory and create a new file called Countdown.js. This is where we will define our countdown component.

  2. Open Countdown.js in your text editor and add the following code:

     import React, { useState, useEffect } from 'react';
    
     const Countdown = () => {
       const [count, setCount] = useState(10);
    
       useEffect(() => {
         const countdown = setInterval(() => {
           setCount((prevCount) => prevCount - 1);
         }, 1000);
    
         return () => {
           clearInterval(countdown);
         };
       }, []);
    
       return (
         <div>
           <h1>Countdown Timer</h1>
           <p>{count}</p>
         </div>
       );
     };
    
     export default Countdown;
    

    In this code, we are using the useState and useEffect hooks from React. The count state variable holds the current count value, and the setCount function allows us to update it.

    The useEffect hook is responsible for starting and stopping the countdown. It runs once when the component is mounted, thanks to the empty dependency array ([]). Inside the useEffect hook, we use setInterval to decrement the count by 1 every second. Finally, we clean up the interval in the cleanup function returned by useEffect to avoid memory leaks.

  3. Save the file.

Step 3: Using the Countdown Component

  1. Open the src directory and edit the App.js file.

  2. Remove the existing code in App.js and replace it with the following:

     import React from 'react';
     import Countdown from './Countdown';
    
     const App = () => {
       return (
         <div className="App">
           <Countdown />
         </div>
       );
     };
    
     export default App;
    

    This code imports the Countdown component we created and renders it inside the App component.

  3. Save the file.

  4. Go to your browser and you should see the countdown timer running!

Conclusion

In this tutorial, we learned how to create a countdown timer using React.js. We utilized React's useState and useEffect hooks to manage the state of our application and update the count value every second. React's component-based architecture made it easy to create reusable and modular code. Feel free to customize the countdown timer further or add additional features to suit your needs. Happy coding!

More from this blog

D

Dristanta Silwal - Hashnode

39 posts

Hello! I'm a passionate and inquisitive computer science student with a knack for exploring new horizons. I'm constantly seeking opportunities to expand my knowledge and skills.