{"id":51276,"date":"2024-05-08T16:55:09","date_gmt":"2024-05-08T11:25:09","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=51276"},"modified":"2026-01-07T12:20:23","modified_gmt":"2026-01-07T06:50:23","slug":"steps-to-create-stopwatch-using-reactjs","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/steps-to-create-stopwatch-using-reactjs\/","title":{"rendered":"Stopwatch Using ReactJS: 4 Easy Steps to Create Your First React Application"},"content":{"rendered":"\n<p>One of the hard-hitting truths is that <strong><em>&#8220;Creating simple projects is the easiest way to learn a tool instead of theoretically reading it thousands of times&#8221;.<\/em><\/strong><\/p>\n\n\n\n<p>If you are a beginner and if you want to learn something, go for a practical approach than a theoretical approach. In that way, if you are looking to learn one of the most famous tools, ReactJS, then <strong><em>how about you learn it by creating a stopwatch using ReactJS?<\/em><\/strong><\/p>\n\n\n\n<p>That is what we&#8217;ll be covering in this article. An <strong>easy 4-step method to create a stopwatch using ReactJS<\/strong> covering from scratch so you don&#8217;t have to worry even if you are a beginner to this.<\/p>\n\n\n\n<p>So, let us get started and create your first project which is the stopwatch using ReactJS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites for Creating a Stopwatch Using ReactJS<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"628\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-1200x628.webp\" alt=\"Prerequisites for Creating a Stopwatch Using ReactJS\" class=\"wp-image-52293\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-1200x628.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-768x402.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-1536x804.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-2048x1072.webp 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/2_prerequisites_for_creating_a_stopwatch_using_reactjsr-150x79.webp 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Let&#8217;s take a detour before you dive into building a stopwatch using ReactJS. There are a <strong>few prerequisites <\/strong>that you need to have in place. <\/p>\n\n\n\n<p>Ensuring you <strong>meet these requirements <\/strong>will make your <strong>development process smoother <\/strong>and more efficient. <\/p>\n\n\n\n<p><strong><em>Also, You Must Know About <a href=\"https:\/\/www.guvi.in\/blog\/mistakes-to-avoid-in-first-react-project\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/mistakes-to-avoid-in-first-react-project\/\" rel=\"noreferrer noopener\">Mistakes to Avoid When Starting the First React Project<\/a><\/em><\/strong><\/p>\n\n\n\n<p>Let\u2019s go through each prerequisite needed to create a Stopwatch using ReactJS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Basic Understanding of ReactJS<\/strong><\/h3>\n\n\n\n<p>To effectively build a stopwatch application, you should have a <strong><a href=\"https:\/\/www.guvi.in\/courses\/web-development\/react-js\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=stopwatch-using-reactjs\" target=\"_blank\" rel=\"noreferrer noopener\">foundational knowledge of ReactJS.<\/a><\/strong> This includes:<\/p>\n\n\n\n<ul>\n<li>JSX<\/li>\n\n\n\n<li>Components<\/li>\n\n\n\n<li>State Management<\/li>\n\n\n\n<li>Lifecycle Methods<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. NodeJS and npm Installed<\/strong><\/h3>\n\n\n\n<p>NodeJS is a JavaScript runtime that lets you run your JavaScript code server-side, and <a href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">npm <\/a>(Node Package Manager) is a tool that <strong>allows you to install JavaScript packages<\/strong> from the npm registry.<\/p>\n\n\n\n<ul>\n<li><strong>Installation<\/strong>: If you haven&#8217;t installed NodeJS and npm yet, you can download them from <a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>nodejs.org<\/strong><\/a>. Installing NodeJS should automatically install npm as well.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Know More About <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-nodejs-as-backend\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/guide-for-nodejs-as-backend\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.JS as Backend: Everything You Need to Know About this Popular Framework <\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Code Editor<\/strong><\/h3>\n\n\n\n<p>A good code editor can <strong>significantly enhance your coding efficiency.<\/strong> Here are a few popular ones that are well-suited for JavaScript and React development:<\/p>\n\n\n\n<ul>\n<li><a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Visual Studio Code (VS Code)<\/strong>: <\/a>Highly recommended for its<strong> extensive ecosystem<\/strong> of extensions, built-in support for JavaScript and React, and an integrated terminal.<\/li>\n\n\n\n<li><strong>Atom<\/strong>: Known for its customizability and being open-source.<\/li>\n\n\n\n<li><strong>Sublime Text<\/strong>: Fast and efficient, though it requires a license for continued use.<\/li>\n<\/ul>\n\n\n\n<p>Make sure your code editor is set up to handle JavaScript and JSX syntax highlighting and has auto-completion features enabled to aid in your development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. A Basic Understanding of HTML and CSS<\/strong><\/h3>\n\n\n\n<p>While the focus of your project is on ReactJS, having a <strong><a href=\"https:\/\/www.guvi.in\/courses\/web-development\/modern-html-css\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=stopwatch-using-reactjs\" target=\"_blank\" rel=\"noreferrer noopener\">basic understanding of HTML and CSS <\/a><\/strong>will be crucial since you will need to style your stopwatch component and understand the document structure.<\/p>\n\n\n\n<ul>\n<li><strong>HTML<\/strong>: Know how to use various HTML tags and their attributes.<\/li>\n\n\n\n<li><strong>CSS<\/strong>: Understand how to style your elements, including layout techniques like Flexbox or Grid, which can help in positioning your stopwatch on the page.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Familiarity with Command Line<\/strong><\/h3>\n\n\n\n<p>You\u2019ll be using the command line to run various commands for creating your React app, starting the development server, and installing packages.<\/p>\n\n\n\n<ul>\n<li><strong><a href=\"https:\/\/gist.github.com\/bradtraversy\/cc180de0edee05075a6139e42d5f28ce\" target=\"_blank\" rel=\"noreferrer noopener\">Basic Commands: <\/a><\/strong>Be comfortable navigating directories, running scripts, and managing files from the command line.<\/li>\n<\/ul>\n\n\n\n<p>If you are through with these prerequisites, then it is time for you to create your stopwatch using ReactJS<\/p>\n\n\n\n<p><strong><em>Learn more: <a href=\"https:\/\/www.guvi.in\/blog\/a-complete-guide-to-html-and-css-for-beginners\/\" target=\"_blank\" rel=\"noreferrer noopener\">A Complete Guide to HTML and CSS for Beginners<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create a Stopwatch Using ReactJS in 4-Steps<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"628\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-1200x628.webp\" alt=\"Create a Stopwatch Using ReactJS in 4-Steps\" class=\"wp-image-52294\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-1200x628.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-768x402.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-1536x804.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-2048x1072.webp 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/3_create_a_stopwatch_using_reactjs_in_4_steps-150x79.webp 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Now that the prerequisites are set up, let us jump to our main topic of the day, which is to create a stopwatch using ReactJS.<\/p>\n\n\n\n<p>But, before you indulge further, it is a must that you know the basics of full-stack development. If not, enroll yourself in a<strong> professionally <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=stopwatch-using-reactjs\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Certified Online Full-stack Development Course<\/strong> <\/a>by a recognized institution that will not only strengthen your basic but provide you with an industry-grade certificate.<\/p>\n\n\n\n<p>Let us now see how to create a stopwatch using ReactJS in 4-steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step<\/strong> <strong>1. Setting Up Your React Environment <\/strong><\/h3>\n\n\n\n<p>Before you can start building your stopwatch using ReactJS, you&#8217;ll need a <strong>suitable development environment <\/strong>for ReactJS. <\/p>\n\n\n\n<p>Setting this up is straightforward, but vital to <strong>ensure everything runs smoothly<\/strong> as you develop. <\/p>\n\n\n\n<p><strong><em>Also Explore: <a href=\"https:\/\/www.guvi.in\/blog\/build-a-search-filter-component-in-react\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/build-a-search-filter-component-in-react\/\" rel=\"noreferrer noopener\">Build a Search Component in React [just in 3 simple steps]<\/a><\/em><\/strong><\/p>\n\n\n\n<p>Let\u2019s break down the process step by step.<\/p>\n\n\n\n<p><strong>1. Create a New React Application<\/strong><\/p>\n\n\n\n<p>After you install NodeJS and npm as mentioned in the previous section, you can use the<strong> Create React App CLI tool <\/strong>to set up a new React project. <\/p>\n\n\n\n<p>Create React App is an officially supported way to create single-page React applications. It offers a <strong>modern build setup<\/strong> with no configuration.<\/p>\n\n\n\n<ul>\n<li><strong>Run Create React App<\/strong>: Open your terminal, navigate to the directory where you want your project to be, and run:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>npx create-react-app stopwatch-app<\/strong><\/code><\/code><\/pre>\n\n\n\n<p><code><strong>npx<\/strong><\/code> is a <strong>package runner tool <\/strong>that comes with npm 5.2+ and higher. It will download the <strong>most recent Create React App<\/strong> release, build a new project named <code><strong>stopwatch-app<\/strong><\/code>, and install all dependencies. It will take a couple of minutes to install all the required dependencies. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"577\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-1200x577.webp\" alt=\"create react app\" class=\"wp-image-51283\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-1200x577.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-300x144.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-768x369.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-1536x738.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831-150x72.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090831.webp 1908w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"605\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-1200x605.webp\" alt=\"create react app\" class=\"wp-image-51282\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-1200x605.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-300x151.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-768x387.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-1536x774.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918-150x76.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-090918.webp 1919w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Once done, you will see something like this and now navigate to your project directory.<\/p>\n\n\n\n<ul>\n<li><strong>Navigate into your project directory<\/strong>: Once the setup process is complete, you need to move into your project directory:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>cd stopwatch-app<\/strong><\/code><\/code><\/pre>\n\n\n\n<p><strong><em>Know <a href=\"https:\/\/www.guvi.in\/blog\/how-to-fetch-and-display-data-from-api-in-react\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/how-to-fetch-data-using-api-in-react\/\" rel=\"noreferrer noopener\">How to Use ReactJS to Fetch and Display Data from API \u2013 5 Simple Steps<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong>2. Familiarize Yourself with the Project Structure<\/strong><\/p>\n\n\n\n<p>When you open your new <code><strong>stopwatch-app<\/strong><\/code> directory in your favorite code editor, you\u2019ll see several files and folders:<\/p>\n\n\n\n<ul>\n<li><strong>node_modules\/<\/strong>: Contains all packages that npm installed.<\/li>\n\n\n\n<li><strong>public\/<\/strong>: Holds static assets like the HTML file and images.<\/li>\n\n\n\n<li><strong>src\/<\/strong>: Where your JavaScript files and components will live. This is where you will spend most of your time.<\/li>\n\n\n\n<li><strong>package.json<\/strong>: Manages your project\u2019s dependencies, scripts, and versions.<\/li>\n\n\n\n<li><strong>README.md<\/strong>: Provides information about scripts and the project structure.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/how-to-render-an-array-of-objects-in-react\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/how-to-render-an-array-of-objects-in-react\/\" rel=\"noreferrer noopener\">How to Render an Array of Objects in React? [in 3 easy steps]<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong>3. Start the Development Server<\/strong><\/p>\n\n\n\n<p>To view your project in a browser, you need to <strong>start the development server.<\/strong> Still in your terminal, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>npm start<\/strong><\/code><\/code><\/pre>\n\n\n\n<p>This command starts a development server and <strong>opens up your new React application in your default web browser.<\/strong> The page will reload if you make edits, and you will see build errors and lint warnings in the console.<\/p>\n\n\n\n<p>Now that your environment is set up, you\u2019re ready to <strong>start coding your stopwatch using ReactJS<\/strong>. This setup process ensures you have a solid foundation for not only this project but any future React projects you might undertake. Let\u2019s now build a stopwatch using ReactJS.<\/p>\n\n\n\n<p><strong><em>Explore: <a href=\"https:\/\/www.guvi.in\/blog\/guide-on-ruby-on-rails-vs-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ruby on Rails vs JavaScript: A Comprehensive Analysis for Backend Development<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Creating the Stopwatch Component <\/strong><\/h3>\n\n\n\n<p>Once your React environment is set up, the next step is to create the actual stopwatch component. This component will<strong> handle everything from timing to user interactions <\/strong>for starting, stopping, and resetting the stopwatch. <\/p>\n\n\n\n<p>Let&#8217;s break down each part of this process.<\/p>\n\n\n\n<p><strong>1. Creating the Stopwatch File<\/strong><\/p>\n\n\n\n<p>First, you\u2019ll need a place to write your JavaScript logic for the stopwatch. Here\u2019s how to set that up:<\/p>\n\n\n\n<ul>\n<li><strong>Create the file<\/strong>: In your React application\u2019s <code><strong>src<\/strong><\/code> directory, create a new file named <code><strong>Stopwatch.js<\/strong><\/code>. This file will house all the code related to your stopwatch component.<\/li>\n\n\n\n<li><strong>Open the file<\/strong>: Open <code><strong>Stopwatch.js<\/strong><\/code> in your code editor to start coding your component.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Setting Up the Component<\/strong><\/p>\n\n\n\n<p>To build your stopwatch using ReactJS, you will start by<strong> setting up the basic structure of a React<\/strong> functional component and importing the necessary hooks.<\/p>\n\n\n\n<ul>\n<li><strong>Import React and Hooks<\/strong>: At the top of your file, you need to import React and the hooks you will use:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState, useEffect } from 'react';<\/code><\/pre>\n\n\n\n<p><strong><code>useState<\/code> <\/strong>is used for managing the state of your component, while <code><strong>useEffect<\/strong><\/code> is for handling lifecycle events and side effects.<\/p>\n\n\n\n<ul>\n<li><strong>Initialize the Component<\/strong>: Below the imports, declare your stopwatch component:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><code>function Stopwatch() { <\/code>\n<\/strong><code>\/\/ Component logic and JSX will go here<\/code><strong><code> }<\/code><\/strong><\/code><\/pre>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/usestate-hook-in-react-for-beginners-react-hooks\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/usestate-hook-in-react-for-beginners-react-hooks\/\">useState() Hook in React for Beginners | React Hooks<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong>3. Managing State with <code>useState<\/code><\/strong><\/p>\n\n\n\n<p>Your stopwatch needs to keep track of a few pieces of state: the elapsed time and whether the stopwatch is running.<\/p>\n\n\n\n<ul>\n<li><strong>Define State Variables<\/strong>: Inside your<strong> <code>Stopwatch<\/code><\/strong> function, use the <code><strong>useState<\/strong><\/code> hook to create state variables:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;time, setTime] = useState(0);\nconst &#91;isRunning, setIsRunning] = useState(false);<\/code><\/pre>\n\n\n\n<p><code><strong>time<\/strong><\/code> holds the elapsed time in milliseconds, and <code><strong>isRunning<\/strong><\/code> is a boolean that tracks if the stopwatch is active.<\/p>\n\n\n\n<p><strong>4. Handling Time with <code>useEffect<\/code><\/strong><\/p>\n\n\n\n<p>To make the stopwatch operational, you need to increment the <code><strong>time<\/strong><\/code> state every 10 milliseconds when it is running. This is where <strong><code>useEffect<\/code> <\/strong>comes in.<\/p>\n\n\n\n<ul>\n<li><strong>Set Up the Interval<\/strong>: Use <code>useEffect<\/code> to start an interval when the stopwatch is running:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>useEffect(() =&gt; {\n    let interval;\n    if (isRunning) {\n        interval = setInterval(() =&gt; {\n            setTime(prevTime =&gt; prevTime + 10);\n        }, 10);\n    } else {\n        clearInterval(interval);\n    }\n    return () =&gt; clearInterval(interval);\n}, &#91;isRunning]);<\/code><\/pre>\n\n\n\n<p>This effect sets up an interval that <strong>updates <code>time<\/code> every 10 milliseconds<\/strong>, but only when <code>isRunning<\/code> is true. When <code>isRunning<\/code> changes to false, or the component unmounts, the interval is cleared.<\/p>\n\n\n\n<p><strong><em>Know About <a href=\"https:\/\/www.guvi.in\/blog\/how-to-setup-react-router-v6-tutorial\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/how-to-setup-react-router-v6\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Setup React Router v6? | Tutorial<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong>5. Building the Component&#8217;s UI<\/strong><\/p>\n\n\n\n<p>Now, it\u2019s time to define what your stopwatch will look like.<\/p>\n\n\n\n<ul>\n<li><strong>Render the Stopwatch<\/strong>: Below your hooks and logic, define the JSX that will be returned by your component:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\n    &lt;div&gt;\n        &lt;h1&gt;{(\"0\" + Math.floor((time \/ 60000) % 60)).slice(-2)}:\n        {(\"0\" + Math.floor((time \/ 1000) % 60)).slice(-2)}:\n        {(\"0\" + ((time \/ 10) % 100)).slice(-2)}&lt;\/h1&gt;\n        &lt;button onClick={() =&gt; setIsRunning(true)}&gt;Start&lt;\/button&gt;\n        &lt;button onClick={() =&gt; setIsRunning(false)}&gt;Stop&lt;\/button&gt;\n        &lt;button onClick={() =&gt; setTime(0)}&gt;Reset&lt;\/button&gt;\n    &lt;\/div&gt;\n);\n<\/code><\/pre>\n\n\n\n<p>This code renders the stopwatch display and three buttons for controlling it. The time display is formatted as <strong>minutes, seconds, and hundredths of a second.<\/strong><\/p>\n\n\n\n<p><strong>6. Export the Component<\/strong><\/p>\n\n\n\n<p>Finally, don\u2019t forget to export your component so it can be used in other parts of your application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>export default Stopwatch;<\/strong><\/code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"733\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-1200x733.webp\" alt=\"create react app\" class=\"wp-image-51290\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-1200x733.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-300x183.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-768x469.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-1536x938.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330-150x92.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-092330.webp 1586w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>This is how your code to build a stopwatch using ReactJS would look like when you compile everything. <\/p>\n\n\n\n<p>The full code for the Stopwatch component will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState, useEffect } from 'react';\nfunction Stopwatch() {\n    \/\/ Component logic and JSX will go here\n    const &#91;time, setTime] = useState(0);\n    const &#91;isRunning, setIsRunning] = useState(false);\n\n    useEffect(() =&gt; {\n        let interval;\n        if (isRunning) {\n            interval = setInterval(() =&gt; {\n                setTime(prevTime =&gt; prevTime + 10);\n            }, 10);\n        } else {\n            clearInterval(interval);\n        }\n        return () =&gt; clearInterval(interval);\n    }, &#91;isRunning]);\n\n    return (\n    &lt;div&gt;\n        &lt;h1&gt;{(\"0\" + Math.floor((time \/ 60000) % 60)).slice(-2)}:\n        {(\"0\" + Math.floor((time \/ 1000) % 60)).slice(-2)}:\n        {(\"0\" + ((time \/ 10) % 100)).slice(-2)}&lt;\/h1&gt;\n        &lt;button onClick={() =&gt; setIsRunning(true)}&gt;Start&lt;\/button&gt;\n        &lt;button onClick={() =&gt; setIsRunning(false)}&gt;Stop&lt;\/button&gt;\n        &lt;button onClick={() =&gt; setTime(0)}&gt;Reset&lt;\/button&gt;\n    &lt;\/div&gt;\n);\n\n}\nexport default Stopwatch;\n<\/code><\/pre>\n\n\n\n<p>You&#8217;ve now set up the basic functionality of your stopwatch using ReactJS. This setup involves <em>managing the state with hooks, updating the state based on user interactions, and rendering the UI based on the current state.<\/em> <\/p>\n\n\n\n<p>With the stopwatch component in place, you\u2019re ready to build more interactive and dynamic applications like stopwatch using ReactJS.<\/p>\n\n\n\n<p><strong><em>Read more: <a href=\"https:\/\/www.guvi.in\/blog\/top-technologies-for-javascript-backend-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Top Technologies to Learn for a JavaScript Backend Developer<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Integrating the Stopwatch Component<\/strong><\/h3>\n\n\n\n<p>Now that you have created the <strong><code>Stopwatch<\/code> <\/strong>component, it&#8217;s time to integrate it into your React application so that it becomes part of the user interface. <\/p>\n\n\n\n<p>This step is crucial to build a stopwatch using ReactJS because it brings your component to life, allowing users to interact with it directly through the UI of your application. <\/p>\n\n\n\n<p>Let&#8217;s go through how you can achieve this.<\/p>\n\n\n\n<p><strong>1. Importing the Stopwatch Component<\/strong><\/p>\n\n\n\n<p>Firstly, you need to ensure that the <code><strong>Stopwatch<\/strong><\/code> component is accessible where you intend to use it, typically in your application&#8217;s main component or another component of your choice.<\/p>\n\n\n\n<ul>\n<li><strong>Open the App Component<\/strong>: Navigate to the <code>src<\/code> folder and open the<strong> App.js <\/strong>file. This file usually serves as the root component in a simple React application created with the Create React App.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>Import Stopwatch<\/strong>: At the top of <code><strong>App.js<\/strong><\/code>, add an import statement to bring in the <strong><code>Stopwatch<\/code> <\/strong>component. Your import should look like this:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code><strong>import Stopwatch from '.\/Stopwatch';<\/strong><\/code><\/code><\/pre>\n\n\n\n<p><strong><em>Explore <a href=\"https:\/\/www.guvi.in\/blog\/how-to-use-props-in-react\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/how-to-use-props-in-react\/\" rel=\"noreferrer noopener\">How to use Props in React [in 3 simple steps]<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong>2. Adding the Stopwatch to the Render Method<\/strong><\/p>\n\n\n\n<p>With the <code>Stopwatch<\/code> component imported, you can now include it in your application&#8217;s render output. This is where you decide how the stopwatch is displayed relative to other content in your app.<\/p>\n\n\n\n<ul>\n<li><strong>Modify the JSX<\/strong>: In the <code><strong>App.js<\/strong><\/code> file, locate the <code><strong>return<\/strong><\/code> statement inside the <code>App<\/code> function. This statement defines what this component will render. You will already have a component written over there, replace that with your <code><strong>Stopwatch<\/strong><\/code> component is given below:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function App() {\n  return (\n    &lt;div className=\"App\"&gt;\n      &lt;header className=\"App-header\"&gt;\n        &lt;Stopwatch \/&gt;\n      &lt;\/header&gt;\n    &lt;\/div&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>Position Your Component<\/strong>: The example above places the <code>Stopwatch<\/code> inside a <code>&lt;header&gt;<\/code> element, which might typically be styled prominently. You can adjust this placement based on your UI design requirements, placing the stopwatch in different parts of your layout or within other components.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Styling the Stopwatch<\/strong><\/p>\n\n\n\n<p>To make sure the stopwatch not only works well but also looks good, consider adding some custom CSS styles. With the Create React App, you will already be equipped with a CSS file with the necessary styling. YOu can change it according to your needs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"716\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-1200x716.webp\" alt=\"create react app\" class=\"wp-image-51292\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-1200x716.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-300x179.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-768x458.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-1536x916.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313-150x89.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093313.webp 1651w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>This is what the code for <strong>App.js <\/strong>looks like. With this, you completed the necessary operations needed to build a stopwatch using ReactJS. Now it is time to test it. <\/p>\n\n\n\n<p>The full code for the integration will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Stopwatch from '.\/Stopwatch';\nimport logo from '.\/logo.svg';\nimport '.\/App.css';\n\nfunction App() {\n  return (\n    &lt;div className=\"App\"&gt;\n      &lt;header className=\"App-header\"&gt;\n        &lt;Stopwatch \/&gt;\n      &lt;\/header&gt;\n    &lt;\/div&gt; \n  );\n\n\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<p><strong><em>Explore More: <a href=\"https:\/\/www.guvi.in\/blog\/php-or-nodejs-framework-for-backend\/\" target=\"_blank\" rel=\"noreferrer noopener\">PHP or NodeJS: Which Framework is Better For Backend<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step<\/strong> <strong>4. Testing Your Integration<\/strong><\/h3>\n\n\n\n<p>Once you have placed and styled your Stopwatch using ReactJS, it&#8217;s important to test it:<\/p>\n\n\n\n<ul>\n<li><strong>Run Your App<\/strong>: Ensure your development server is running (use <code>npm start<\/code> if it&#8217;s not). Open your application in a browser to see the stopwatch in action.<\/li>\n\n\n\n<li><strong>Interact with the Stopwatch<\/strong>: Click the start, stop, and reset buttons to ensure that the stopwatch behaves as expected. Watch for any UI issues or bugs in functionality.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"643\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-1200x643.webp\" alt=\"stopwatch using ReactJS\" class=\"wp-image-51293\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-1200x643.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-300x161.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-768x411.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-1536x823.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522-150x80.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-08-093522.webp 1919w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>This is the output that you would get when you run the stopwatch using ReactJS. You can control this by clicking the start, stop, and reset buttons. This is a simple application built using ReactJS that is highly effective for you to strengthen your basics on ReactJS. <\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>If you want to learn more about projects like this that are built using ReactJS, then consider enrolling in<\/em><br><em>HCL<\/em> <em>GUVI\u2019s<strong> <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=stopwatch-using-reactjs\">Full-Stack Development Course<\/a>\u00a0<\/strong>not only gives you theoretical knowledge but also practical knowledge with the help of real-world projects.<\/em><\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/react-project-ideas-for-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">10 Best React Project Ideas for Developers [with Source Code]<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, creating a stopwatch using ReactJS involves setting up your development environment, building the stopwatch component with state and effects, integrating it into your main application, and running your app to see it in action. <\/p>\n\n\n\n<p>By following these steps, you can develop a functional and interactive stopwatch using ReactJS. This project not only enhances your understanding of React&#8217;s fundamental concepts but also provides a practical application that showcases your skills in modern web development.<\/p>\n\n\n\n<p><strong><em>Must Explore Top ReactJS Interview Questions and Answers Of 2026! <a href=\"https:\/\/www.guvi.in\/blog\/most-important-react-interview-questions\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/most-important-react-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">[Part-1]<\/a>\/<a href=\"https:\/\/www.guvi.in\/blog\/important-react-interview-questions\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/important-react-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">[Part-2]<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1715141566311\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why build a stopwatch using ReactJS?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>ReactJS makes it easy to manage state and effects, which are essential for accurately tracking and displaying time in a stopwatch application.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1715141591957\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are the essential hooks needed for a React stopwatch?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>useState<\/strong> for managing time and running state, and <strong>useEffect <\/strong>for setting up the timer interval.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1715141613132\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I add start, stop, and reset functionality to the stopwatch?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Handle these actions with buttons that modify the state, such as setting <strong>isRunning <\/strong>to true or false, and resetting time to zero.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1715141631496\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are some common bugs to look out for when developing a stopwatch using ReactJS?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Common issues when building a stopwatch using ReactJS include multiple intervals running at once, incorrect time formatting, or state updates not rendering correctly.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>One of the hard-hitting truths is that &#8220;Creating simple projects is the easiest way to learn a tool instead of theoretically reading it thousands of times&#8221;. If you are a beginner and if you want to learn something, go for a practical approach than a theoretical approach. In that way, if you are looking to [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":71718,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294,429],"tags":[],"views":"9979","authorinfo":{"name":"Lukesh S","url":"https:\/\/www.guvi.in\/blog\/author\/lukesh\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Stopwatch-Using-ReactJS_\u20284-Easy-Steps-to-Create-Your-First-React-Application-1-300x116.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/05\/Stopwatch-Using-ReactJS_\u20284-Easy-Steps-to-Create-Your-First-React-Application-1.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/51276"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=51276"}],"version-history":[{"count":42,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/51276\/revisions"}],"predecessor-version":[{"id":98464,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/51276\/revisions\/98464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/71718"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=51276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=51276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=51276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}