Build a Stopwatch using React in 20 minutes! ⏱

Поделиться
HTML-код
  • Опубликовано: 28 дек 2024

Комментарии •

  • @BroCodez
    @BroCodez  11 месяцев назад +28

    import Stopwatch from './Stopwatch.jsx';
    function App() {
    return ();
    }
    export default App;
    import React, {useState, useEffect, useRef} from 'react';
    function Stopwatch(){
    const [isRunning, setIsRunning] = useState(false);
    const [elapsedTime, setElapsedTime] = useState(0);
    const intervalIdRef = useRef(null);
    const startTimeRef = useRef(0);
    useEffect(() => {
    if(isRunning){
    intervalIdRef.current = setInterval(() => {
    setElapsedTime(Date.now() - startTimeRef.current);
    }, 10);
    }
    return () => {
    clearInterval(intervalIdRef.current);
    }
    }, [isRunning]);
    function start(){
    setIsRunning(true);
    startTimeRef.current = Date.now() - elapsedTime;
    }
    function stop(){
    setIsRunning(false);
    }
    function reset(){
    setElapsedTime(0);
    setIsRunning(false);
    }
    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 `${minutes}:${seconds}:${milliseconds}`;
    }
    return(

    {formatTime()}

    Start
    Stop
    Reset


    );
    }
    export default Stopwatch
    body{
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: hsl(0, 0%, 95%);
    }
    .stopwatch{
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 5px solid;
    border-radius: 50px;
    background-color: white;
    padding: 30px;
    }
    .display{
    font-size: 5rem;
    font-family: monospace;
    font-weight: bold;
    color: hsl(0, 0%, 30%);
    text-shadow: 2px 2px 2px hsla(0, 0%, 0%, 0.75);
    margin-bottom: 25px;
    }
    .controls button{
    font-size: 1.5rem;
    font-weight: bold;
    padding: 10px 20px;
    margin: 5px;
    min-width: 125px;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    color: white;
    transition: background-color 0.5s ease;
    }
    .start-button{
    background-color: hsl(115, 100%, 40%);
    }
    .start-button:hover{
    background-color: hsl(115, 100%, 35%);
    }
    .stop-button{
    background-color: hsl(10, 90%, 50%);
    }
    .stop-button:hover{
    background-color: hsl(10, 90%, 45%);
    }
    .reset-button{
    background-color: hsl(205, 90%, 60%);
    }
    .reset-button:hover{
    background-color: hsl(205, 90%, 55%);
    }

    • @RealRower1
      @RealRower1 11 месяцев назад +1

      hey Bro Code can you make a course about lua @BroCodez

    • @avanguly4843
      @avanguly4843 11 месяцев назад +1

      i dont think he plays roblox lil bro@@RealRower1

    • @l213dhanesh3
      @l213dhanesh3 11 месяцев назад +1

      Where are u code bro help me with angular js..... Missing u bro mava

    • @-_SleepAndEat_-
      @-_SleepAndEat_- 8 месяцев назад

      Bro can you do Kali Linux ?

  • @YoniYo12
    @YoniYo12 8 месяцев назад +4

    Best project tutorial ever! simple and to the point, thanks Bro

  • @syedmajid3202
    @syedmajid3202 20 дней назад

    Thanks a lot, i completed your react course today, i learned alot from your tutorials, keep making new videos! 👍👍👍

  • @ejebeueh9113
    @ejebeueh9113 11 месяцев назад +58

    So this bro gets 830 views while dancing toys get millions what a shame

  • @Dylan12kpo1k2pDylan
    @Dylan12kpo1k2pDylan 11 месяцев назад +12

    You beautiful man, keep adding to this playlist

  • @tofu7736
    @tofu7736 11 месяцев назад +5

    Just waiting for 12:00:00 hours React video to come
    By the way I have watched all the web development courses you’ve been uploading.
    Thanks bro because of you i can understand and learn web dev.
    Big heart for BRO ARMY 💜💜💜💜💜

    • @nesa6582
      @nesa6582 11 месяцев назад +1

      Its over, it is only 4h. :D

  • @fahimsium1013
    @fahimsium1013 3 месяца назад

    Respect and love,brother.
    timer func is really crazy with setInterval

  • @abdulbasit9364
    @abdulbasit9364 8 месяцев назад +1

    Bro you drop ur crown 👑❤

  • @syedmajid3202
    @syedmajid3202 3 месяца назад

    Thanks a lot bro, keep making new videos, please make a tutorials on node.js and express.js!

  • @rishabhkedia9304
    @rishabhkedia9304 11 месяцев назад +4

    Please make Reactjs projects as well

  • @samaali6589
    @samaali6589 7 месяцев назад

    Keep it up! Appreciate the efforts

  • @hinatashoyo3891
    @hinatashoyo3891 4 месяца назад

    BRO CAN WE GO AHEAD OF THIS NOW!!!! PLEAASEE

  • @vanshgoyal-uo5cf
    @vanshgoyal-uo5cf 4 месяца назад

    superb tutorial bro

  • @worldnothing1928
    @worldnothing1928 11 месяцев назад +2

    bro are bringing backend development also

  • @ranjithkumar7896
    @ranjithkumar7896 10 месяцев назад +3

    Bro continue the react series, enjoying it try to upload next videos faster

  • @Ashwini243
    @Ashwini243 10 месяцев назад +3

    Hi, I am based in the USA.I really love your videos and they are interview oriented.
    Can you explain why you used useRef variable?

    • @Prateek_Mantry
      @Prateek_Mantry 9 месяцев назад +1

      useRef is used to stop re-rendering your component when the variable(ref) is changed, which is not possible using useState().

  • @yogii1108
    @yogii1108 9 месяцев назад +1

    bro start next js using JavaScript waiting to learn from your channel

  • @Prateek_Mantry
    @Prateek_Mantry 9 месяцев назад +1

    Hi Bro....Please make more videos.

  • @AbdalazezALBOGA
    @AbdalazezALBOGA 9 месяцев назад

    hey bro
    can u make a course about laravel for beginners
    u r so good and now i only can learn anything from you
    thanks a lot

  • @siddhantraj9425
    @siddhantraj9425 8 месяцев назад

    Thanks man. Helped a lot

  • @ideology8323
    @ideology8323 4 месяца назад

    Thank you bro!

  • @arkartm9989
    @arkartm9989 7 месяцев назад

    True Chad bro..❤

  • @HabibHaris-fe7fy
    @HabibHaris-fe7fy 10 месяцев назад +1

    please sir make a playlist on PANDAS, NUMPY please🌸
    🌺🌼

  • @java_learn6447
    @java_learn6447 10 месяцев назад +1

    Sir please upload react form validations

  • @gayantha6654
    @gayantha6654 19 дней назад

    why elapsed time declared as a state variable? cannot use ref variable instead??

  • @tasfin660
    @tasfin660 7 месяцев назад

    I want more react videos

  • @astartesidon609
    @astartesidon609 7 месяцев назад

    so we can conclude that even if we stop the countdown for a couple of minutes still the time in between when we restart still gonna carry over because we based our elapsed time only on a date?

  • @Delicatamente
    @Delicatamente 8 месяцев назад +2

    So, technicaly, timer re-renders every 10 miliseconds, just because of changing useState variable "elapsedTime" in Stopwatch component, what provide full re-render of Stopwatch component?
    Or re-render happens as a result of changing that useState variable "elapsedTime", which is mentioned in function "formatTime", which (this function) is mapped to timer component? And because of this bound React re-renders component?
    Can someone explain me this technical details?

    • @IgorTr
      @IgorTr 10 дней назад

      every useState update causes a re-render. So in a case like this, where you have to re-render a lot, it would be best to try and make the part of your app that relies on that state as simple and tiny as possible, so you don't re-render more parts of your app than you have to.

  • @mohammedshehinthoombil1610
    @mohammedshehinthoombil1610 5 месяцев назад +1

    can you make redux tutorial

  • @its-SK
    @its-SK 11 месяцев назад +3

    hey bro code I am facing difficulties while learning js what should i do?

    • @1CDzy
      @1CDzy 11 месяцев назад

      look at the documentation on JS, maybe it will help

    • @anandv1391
      @anandv1391 8 месяцев назад

      Mbuf

  • @1CDzy
    @1CDzy 11 месяцев назад +1

    bro, please do C# on NET. development

  • @RealRower1
    @RealRower1 11 месяцев назад +2

    hey Bro Code can you make a course about lua

    • @RealRower1
      @RealRower1 11 месяцев назад

      ye but atleast he has something to post about and i think he is gonna get alot of views because lot of people want to learn lua for game dev in roblox and there is not a good course to learning it so he will be the first

  • @abhishekagrawal1236
    @abhishekagrawal1236 7 дней назад

    Is it full course?

  • @BishanTamang-rk5ji
    @BishanTamang-rk5ji 11 месяцев назад +2

    Make java 2024 plz bro code love from Nepal 💓💓

  • @ejebeueh9113
    @ejebeueh9113 11 месяцев назад +1

    Bro please i need help I just started learning jave script and I'm watching your full course so when I linked my css ,js and HTML and tried the same code that you wrote and opened it with live server nothing appeared on the web and its console it says error404
    Sorry for my poor words
    I hope that you understand what I mean, thanks

    • @aman1589
      @aman1589 11 месяцев назад

      did u install live server?

  • @udoydas6513
    @udoydas6513 5 месяцев назад

    bro advance topics please

  • @reynaldobuscagan7731
    @reynaldobuscagan7731 8 месяцев назад

    Hello, I'm a beginner I wonder what app you use, is it html or eclipse?

  • @_amonimus_
    @_amonimus_ 9 месяцев назад

    bro how many languages do you know

  • @something636-rr
    @something636-rr 11 месяцев назад

    Hey i just wanted to say your last 5 videos don't appear in my sub feed. What is going on

  • @Beanbag59
    @Beanbag59 10 месяцев назад

    Do u tutor

  • @jacklagos936
    @jacklagos936 11 месяцев назад

    Bring parallax plz