<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nonish J.</title>
    <description>The latest articles on DEV Community by Nonish J. (@nonish).</description>
    <link>https://dev.to/nonish</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F971732%2F52b93693-2941-4258-b52c-e4eed2e24152.jpg</url>
      <title>DEV Community: Nonish J.</title>
      <link>https://dev.to/nonish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nonish"/>
    <language>en</language>
    <item>
      <title>How to create a Stopwatch in React ⏱️?</title>
      <dc:creator>Nonish J.</dc:creator>
      <pubDate>Tue, 16 Jan 2024 06:01:25 +0000</pubDate>
      <link>https://dev.to/nonish/create-a-stopwatch-in-react--651</link>
      <guid>https://dev.to/nonish/create-a-stopwatch-in-react--651</guid>
      <description>&lt;h3&gt;
  
  
  What is Stopwatch ?
&lt;/h3&gt;

&lt;p&gt;A Stopwatch which can be started and stopped by pressing a button, so that you can measure exactly how long something takes.&lt;/p&gt;

&lt;p&gt;In this blog we learn to create stopwatch which show hours, minutes, seconds and milliseconds in ReactJs. By the end of this article, you will have a clear understanding of how to build a functional stopwatch in React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source Code&lt;/strong&gt; : &lt;a href="https://codesandbox.io/p/sandbox/stopwatch-jsnv8h"&gt;codesandbox&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Setup
&lt;/h3&gt;

&lt;p&gt;First, you need to create a React project. In the terminal of your text editor and at your chosen directory, type in this command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app stopwatch-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create initial state :
&lt;/h4&gt;

&lt;p&gt;Create two variable state for determine time and current running status of watch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [time, setTime] = useState(0);
  const [isRunning, setIsRunning] = useState(false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add handlers for start &amp;amp; stop and reset :
&lt;/h4&gt;

&lt;p&gt;Lets create two onClick handlers one for play and stop the watch and other to reset the watch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const onHandlePlayAndStop = () =&amp;gt; {
    setIsRunning((pre) =&amp;gt; !pre);
  };

  const onHandleReset = () =&amp;gt; {
    setIsRunning(false);
    setTime(0);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add time logic :
&lt;/h4&gt;

&lt;p&gt;Lets take variables to show hours, minutes ,seconds and milliseconds apply with logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const hours = Math.floor(time / 360000);
  const mins = Math.floor((time % 36000) / 6000);
  const secs = Math.floor((time % 6000) / 100);
  const milliSecs = Math.floor(time % 100);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add UI to show watch with styles.
&lt;/h4&gt;

&lt;p&gt;Now, create watch and show all time varialbes and add padStart() function to show double numbers on screen like exact as a stopwatch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Stopwatch.jsx
 &amp;lt;div className="stopwatch-container"&amp;gt;
      &amp;lt;div className="stopwatch-wrapper"&amp;gt;
        &amp;lt;h2&amp;gt;Stopwatch ⏱️&amp;lt;/h2&amp;gt;
        &amp;lt;div className="stopwatch-timer"&amp;gt;
          {hours.toString().padStart(2, "0")}:{mins.toString().padStart(2, "0")}
          :{secs.toString().padStart(2, "0")}:
          {milliSecs.toString().padStart(2, "0")}
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="stopwatch-btn-wrapper"&amp;gt;
          &amp;lt;button onClick={onHandlePlayAndStop} className="stopwatch-btn"&amp;gt;
            {isRunning ? "Pause" : "Play"}
          &amp;lt;/button&amp;gt;
          &amp;lt;button onClick={onHandleReset} className="stopwatch-btn"&amp;gt;
            Reset
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Adding styles
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Styles.css

body {
  background-image: url("https://images.pexels.com/photos/339119/pexels-photo-339119.jpeg?auto=compress&amp;amp;cs=tinysrgb&amp;amp;w=1260&amp;amp;h=750&amp;amp;dpr=1");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

.stopwatch-wrapper {
  padding: 10px 50px 30px;
  text-align: center;
  background: rgba(255, 255, 255, 0.11);
  border-radius: 16px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(4.9px);
  -webkit-backdrop-filter: blur(4.9px);
  border: 1px solid rgba(255, 255, 255, 0.26);
}

.stopwatch-wrapper h2 {
  color: #fff;
}

.stopwatch-timer {
  font-size: 80px;
  color: #fff;
}

.stopwatch-btn-wrapper {
  display: flex;
  justify-content: space-between;
}

.stopwatch-btn-wrapper button {
  width: 48%;
  border: none;
  border-radius: 5px;
  padding: 10px 0;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}

.stopwatch-btn-wrapper button:first-child {
  background-color: green;
}

.stopwatch-btn-wrapper button:last-child {
  background-color: red;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Managing States using side-effects
&lt;/h4&gt;

&lt;p&gt;To make it working we will use Web-API : setInterval() function and apply inside a useEffect hook and after updating the time state, we will clear the effect by using return clearInterval() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(() =&amp;gt; {
    let intervalCall;
    if (isRunning) {
      intervalCall = setInterval(() =&amp;gt; setTime((pre) =&amp;gt; pre + 1), 10);
    }
    return () =&amp;gt; clearInterval(intervalCall);
  }, [isRunning]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Full Code is here :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Stopwatch.jsx
import { useEffect, useState } from "react";
import "./styles.css";

const Stopwatch = () =&amp;gt; {
  const [time, setTime] = useState(0);
  const [isRunning, setIsRunning] = useState(false);

  useEffect(() =&amp;gt; {
    let intervalCall;
    if (isRunning) {
      intervalCall = setInterval(() =&amp;gt; setTime((pre) =&amp;gt; pre + 1), 10);
    }
    return () =&amp;gt; clearInterval(intervalCall);
  }, [isRunning]);

  const hours = Math.floor(time / 360000);
  const mins = Math.floor((time % 36000) / 6000);
  const secs = Math.floor((time % 6000) / 100);
  const milliSecs = Math.floor(time % 100);

  const onHandlePlayAndStop = () =&amp;gt; {
    setIsRunning((pre) =&amp;gt; !pre);
  };

  const onHandleReset = () =&amp;gt; {
    setIsRunning(false);
    setTime(0);
  };

  return (
    &amp;lt;div className="stopwatch-container"&amp;gt;
      &amp;lt;div className="stopwatch-wrapper"&amp;gt;
        &amp;lt;h2&amp;gt;Stopwatch ⏱️&amp;lt;/h2&amp;gt;
        &amp;lt;div className="stopwatch-timer"&amp;gt;
          {hours.toString().padStart(2, "0")}:{mins.toString().padStart(2, "0")}
          :{secs.toString().padStart(2, "0")}:
          {milliSecs.toString().padStart(2, "0")}
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="stopwatch-btn-wrapper"&amp;gt;
          &amp;lt;button onClick={onHandlePlayAndStop} className="stopwatch-btn"&amp;gt;
            {isRunning ? "Pause" : "Play"}
          &amp;lt;/button&amp;gt;
          &amp;lt;button onClick={onHandleReset} className="stopwatch-btn"&amp;gt;
            Reset
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Stopwatch;

//style.css
body {
  background-image: url("https://images.pexels.com/photos/339119/pexels-photo-339119.jpeg?auto=compress&amp;amp;cs=tinysrgb&amp;amp;w=1260&amp;amp;h=750&amp;amp;dpr=1");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

.stopwatch-wrapper {
  padding: 10px 50px 30px;
  text-align: center;
  background: rgba(255, 255, 255, 0.11);
  border-radius: 16px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(4.9px);
  -webkit-backdrop-filter: blur(4.9px);
  border: 1px solid rgba(255, 255, 255, 0.26);
}

.stopwatch-wrapper h2 {
  color: #fff;
}

.stopwatch-timer {
  font-size: 80px;
  color: #fff;
}

.stopwatch-btn-wrapper {
  display: flex;
  justify-content: space-between;
}

.stopwatch-btn-wrapper button {
  width: 48%;
  border: none;
  border-radius: 5px;
  padding: 10px 0;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}

.stopwatch-btn-wrapper button:first-child {
  background-color: green;
}

.stopwatch-btn-wrapper button:last-child {
  background-color: red;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Result :
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa103ywl3cu1cfu8hgiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa103ywl3cu1cfu8hgiw.png" alt="Stopwatch Ui" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;By apply these steps, you've successfully created a stopwatch in React. And also Understanding the logic behind this for building more complex applications. Thanks for reading.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;h3&gt;
  
  
  Follow Me on Social Media!
&lt;/h3&gt;

&lt;p&gt;If you found this blog helpful, feel free to connect with me on LinkedIn and follow me on github too for more programming tips and tutorials.&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/nonish-jain-707397169"&gt;https://www.linkedin.com/in/nonish-jain-707397169&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Github : &lt;a href="https://github.com/Nonish"&gt;https://github.com/Nonish&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>machinecoding</category>
      <category>ui</category>
    </item>
    <item>
      <title>How to Build a Stepper Component in React 🤔 ?</title>
      <dc:creator>Nonish J.</dc:creator>
      <pubDate>Mon, 15 Jan 2024 19:46:03 +0000</pubDate>
      <link>https://dev.to/nonish/how-to-build-a-stepper-component-in-react--3495</link>
      <guid>https://dev.to/nonish/how-to-build-a-stepper-component-in-react--3495</guid>
      <description>&lt;h2&gt;
  
  
  What is Stepper Component in React ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Stepper component enables the user to create a sequence of logical steps that visualises progress.&lt;/li&gt;
&lt;li&gt;It could also be used for navigation purposes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Key Features
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Display Modes&lt;/strong&gt;—The various display modes allow you to configure the step layout and type.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linear Mode&lt;/strong&gt;—The linear mode requires the user to complete the previous step before proceeding to the next one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orientation&lt;/strong&gt;—You can switch between horizontal and vertical orientation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation&lt;/strong&gt;—You can set the validation logic for each step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Rendering&lt;/strong&gt;—The Stepper allows you to customise the rendering of each step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keyboard Navigation&lt;/strong&gt;—The Stepper supports various keyboard shortcuts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility&lt;/strong&gt;—The Stepper component is accessible by screen readers and provides full WAI-ARIA support.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisite:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/"&gt;NPM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://react.dev/learn"&gt;React JS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sass-lang.com/"&gt;Scss&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;First, you need to create a React project. In the terminal of your text editor and at your chosen directory, type in this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app stepper-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, Go to the directory ‘src’ execute the command prompt there and run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Build and Add Functionality to the Stepper Layout
&lt;/h2&gt;

&lt;p&gt;In the below code, I create the basic stepper look: a rounded circle with a grey background, and a short line right of it. They will both be repeated, depending on the number of steps we want to show.&lt;/p&gt;

&lt;p&gt;For mapping the array we use javascript inbuilt Array() function then use fill() to insert react fragment into it and render the basic div tag. And same create two button for back froth functionality and styled with some basic css.&lt;/p&gt;

&lt;p&gt;And to hide the last div element we use isHide() which return boolean value if mapping index is equal to the no of steps.Then we add onClick handle on both buttons and add functionality with condition within it to back and forth the stepper value. Also added some basic and conditional style to show the stepper effect respectively.&lt;/p&gt;

&lt;p&gt;And at last we pass the props in out Stepper component to get the current value of stepper which will be match with mapping index and show the correct stepper position on srceen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's the final code for the App component:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.jsx

import { Fragment, memo, useState } from "react";
import "./stepper.scss"

const App = () =&amp;gt; {
   const [currentStep, setCurrentStep] = useState(0)
   const NUMBER_OF_STEPS = 5;

   return (
      &amp;lt;div&amp;gt;
         &amp;lt;h1&amp;gt;Stepper : {currentStep}&amp;lt;/h1&amp;gt;
         &amp;lt;Stepper currentStep={currentStep} numberOfSteps={NUMBER_OF_STEPS} /&amp;gt;
         &amp;lt;div className="btn-wrapper"&amp;gt;
            &amp;lt;button onClick={() =&amp;gt; setCurrentStep(pre =&amp;gt; pre === 0 ? pre : pre - 1)}&amp;gt;
               Previous
            &amp;lt;/button&amp;gt;
            &amp;lt;button
               onClick={() =&amp;gt; setCurrentStep(pre =&amp;gt; pre === NUMBER_OF_STEPS ? pre : pre + 1)}&amp;gt;
               Next
            &amp;lt;/button&amp;gt;
         &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
   )
}

export default App;

// eslint-disable-next-line react/prop-types
const Stepper = memo(function Stepper({ currentStep, numberOfSteps }) {
   const isHide = (index) =&amp;gt; index === numberOfSteps - 1;
   return (
      &amp;lt;div className="stepper-wrapper"&amp;gt;
         {Array(numberOfSteps).fill(&amp;lt;&amp;gt;&amp;lt;/&amp;gt;).map((_, index) =&amp;gt; {
            return (&amp;lt;Fragment key={index}&amp;gt;
               &amp;lt;div className={`stepper-circle ${currentStep &amp;gt;= index ? "isActive" : ""}`}&amp;gt;&amp;lt;/div&amp;gt;
               {!isHide(index) &amp;amp;&amp;amp; &amp;lt;div className={`stepper-line  ${currentStep &amp;gt; index ? "isActive" : ""}`}&amp;gt;&amp;lt;/div&amp;gt;}
            &amp;lt;/Fragment&amp;gt;)
         })}
      &amp;lt;/div&amp;gt;
   )
})

//Stepper.jsx

.stepper-wrapper{
   display: flex;
   justify-content: center;
   align-items: center;
}

.stepper-circle{
   width: 60px;
   height: 60px;
   border-radius: 50%;
   background-color: rgba(182, 177, 177, 0.5);
}

.stepper-line{
   width: 40px;
   height: 5px;
   background-color: rgba(182, 177, 177, 0.5);
}

.isActive{
   background-color: aqua;
}

.btn-wrapper{
   display: flex;
   justify-content: space-between;
   margin-top: 50px;
}

.btn-wrapper &amp;gt; button{
   width: 40%;
   border: none;
   border-radius: 5px;
   padding: 10px 20px;
   font-size: 16px;
   font-weight: 700;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5wenluwfup4em3ai3nh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5wenluwfup4em3ai3nh.png" alt="Image description" width="797" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! You've successfully built a stepper component in a React project. The working code demo is here , kindly check out &lt;a href="https://codesandbox.io/p/sandbox/stepper-component-5s9vyw"&gt;codesandbox&lt;/a&gt;. Please let me know your thoughts and suggestions about this article.&lt;/p&gt;

&lt;p&gt;Thanks for reading !!!&lt;/p&gt;

&lt;p&gt;Happy Coding.&lt;/p&gt;

</description>
      <category>react</category>
      <category>scss</category>
      <category>stepper</category>
      <category>machinecoding</category>
    </item>
  </channel>
</rss>
