Top 6 React Hook Mistakes Beginners Make

Поделиться
HTML-код
  • Опубликовано: 3 июл 2024
  • FREE React Hooks Simplified Course: courses.webdevsimplified.com/...
    📚 Materials/References:
    FREE React Hooks Simplified Course: courses.webdevsimplified.com/...
    Reference vs Value Video: • Reference Vs Value In ...
    Reference vs Value Article: blog.webdevsimplified.com/202...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:39 - Using state when you don’t need it
    02:57 - Not using the function version of useState
    06:44 - State does not update immediately
    08:25 - Unnecessary useEffects
    12:40 - Referential equality mistakes
    16:55 - Not aborting fetch requests
    #WebDevelopment #WDS #JavaScript

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

  • @IceMetalPunk
    @IceMetalPunk Год назад +502

    React's own docs explicitly recommend using state-controlled inputs over refs whenever possible. It refers to the ref method as "quick and dirty" because it lets the DOM handle tracking the value instead of React, which can sometimes cause unexpected behavior when React renders its virtual DOM. So... yeah. I think for forms, especially large ones, it's better to keep track of values in a single state object with key-value pairs. That does mean it'll re-render whenever a value changes, but since React only manipulates the DOM for the one input that's changed, it's not a big deal; and it allows React to have more control over, and more understanding of, the inputs, for optimizations.
    The main issue with using local variables instead of useEffect for composite values is future-proofing: when you add more state to the component, those variables will be re-evaluated on every re-render even if they don't need to be. In such cases, I think useMemo is the optimal solution; in fact, it's why useMemo exists! (And I believe recomputing a memoized variable doesn't trigger a re-render the way setting state does, though I couldn't find anything definitive about that.) But you are right that in some cases, you don't need to listen for changes at all, since you can just use the new value at the same time as you're setting it on the state.

    • @SahraClayton
      @SahraClayton Год назад +29

      I think useRef should be used for forms, you don't want component re-rendering on every key stroke just for a form, but if you was using a search/filter input where you are filtering on the users key stroke then you would need to useState and make it a controlled component.

    • @chonmon
      @chonmon Год назад +5

      @@SahraClayton It only re-rendered the input DOM tho? Instead of the whole form if I understand it correctly. I don't think it's a big deal.

    • @ccrsxx
      @ccrsxx Год назад +23

      @@chonmon yes, not to mention you'll always want to have some kinda validation on the form.

    • @SahraClayton
      @SahraClayton Год назад +4

      @@chonmon it's not a big deal but if you can avoid any components from rerendering no matter how small is just a bonus. Also it's less syntax in that component, which looks nicer

    • @thienhuynh7962
      @thienhuynh7962 Год назад +7

      @@SahraClayton then you can opt for other event like submit or blur to minimize re-render. Although useRef is a fine solution since there’s no re-render, using it with forms that have many fields or require validation will give you a hard time managing these fields’ value and their behaviors

  • @jennifermagpantay7933
    @jennifermagpantay7933 Год назад +4

    Golden tips! Love content like that with different case scenarios and clear explanation! I have learnt so much! Thanks for sharing!

  • @jackwin9641
    @jackwin9641 Год назад +1

    well this is what I am looking for ages. THANKS KYLE, you made my day, now I can revise my old code at a higher level

  • @michalbilinski4168
    @michalbilinski4168 Год назад

    Hands down, what a perfect rhetoric - watched it with great pleasure - thanks

  • @Toomda
    @Toomda Год назад

    This was soo helpful. I had a beginner project, just for fun and I almost made all of the mistakes. I just fixed my code, and it looks much better now. Thank you

  • @nsontung
    @nsontung Год назад

    one of the best React tips I ever learn on RUclips. Thank you so much

  • @bowenp01
    @bowenp01 Год назад +15

    Fantastic! I’m just learning react and you’ve explained the funky behaviour I’ve been getting with useState perfectly. Thanks for taking the time to make these videos 😊

    • @rubyvi7308
      @rubyvi7308 Год назад

      Why are you excited to watch while the other crying are u happy

  • @deadlock107
    @deadlock107 Год назад +2

    It was worth to watch, I learned pretty valuable things especially the fetch abort, it's golden, thank you!

  • @runonce
    @runonce Год назад +52

    For handling form why not simply grab the input values from the onSubmit event? No need for state or refs.

    • @twothreeoneoneseventwoonefour5
      @twothreeoneoneseventwoonefour5 Год назад +23

      Yes that is what I usually do.
      const formData = new FormData(e.currentTarget)
      and then
      const [username, password] = [formData.get("username").toString(), formData.get("password").toString()];
      looks cleaner to me than using refs or state there
      well I still use state for input validation though

  • @asmmisfar
    @asmmisfar Год назад +2

    Hey Kyle,
    This is really very helpful for me.
    Tomorrow I have a task to complete in my office.
    I was worried about how to do that.
    but, this video gave me a clear idea about that.
    Thanks a lot. Keep going, bro.
    Loves from Sri Lanka ❤

  • @kamelnazar-instructionalde9740
    @kamelnazar-instructionalde9740 Год назад +1

    This worked incredibly well! I can finally play it thanks

  • @belkocik
    @belkocik Год назад +1

    amazing video for react beginners. :) Thank you. Looking forward for more react mistakes that beginners and more advanced devs make

  • @kuken72
    @kuken72 Год назад

    This was really straight to the point and very helpful. Thank you Kyle! :)

  • @igorkushnir4966
    @igorkushnir4966 Год назад

    last one just perfect! thanks!

  • @WorkThrowaway
    @WorkThrowaway 2 месяца назад

    This was a great video and helped me solve an issue i had with my hook, namely having multiple states update with an action needed once they were both updated. it took a while to wrap my brain around it, but this video really helped give me the vision. love your videos and that you go super in depth (in the longer ones). probably the best coding tutorials on youtube. if i ever get my web dev dream job, i will be getting you a few coffees/beers.

  • @charlesvicencio2461
    @charlesvicencio2461 Год назад

    Thank you Kyle!

  • @darkmift
    @darkmift Год назад +1

    Excellent explanation on state setter usage

  • @MJstart
    @MJstart Год назад

    Thanks for video Kyle

  • @joseff5998
    @joseff5998 Год назад

    I really appreciate your explanations. Top-notch!.

  • @MaherOmri
    @MaherOmri Год назад

    Amazing explanation! Many thanks

  • @DarkFlarePrince
    @DarkFlarePrince Год назад

    This is the single most useful video I've seen since I started React coding

  • @ethanmcrae
    @ethanmcrae Год назад +1

    I really appreciate this video!! 🙌

  • @loicrutabana1884
    @loicrutabana1884 Год назад

    Amazing! You make great content. I especially like your youtube shorts

  • @vaibhavsri.
    @vaibhavsri. Год назад

    Really useful one. Please keep on making great informative videos like this. You're the best!

  • @mystic7207
    @mystic7207 Год назад

    Awesome explanation ❤

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

    Amazing video, thx a lot!

  • @movsesaghabekyan9794
    @movsesaghabekyan9794 Год назад +1

    It was amazing man. Thanks a lot🔥🔥

  • @marwenlabidi_dev
    @marwenlabidi_dev Год назад +2

    thank you for this great quality content

  • @nigeryanes1987
    @nigeryanes1987 Год назад +1

    Excellent video, thank you very much.😊

  • @developer_hadi
    @developer_hadi Год назад +1

    Thank you so much, as a beginner you helped me a lot

  • @wisdomelue
    @wisdomelue Год назад +1

    best explanation of useMemo out here👏🏽

  • @louis-emmanuelisnardon8308
    @louis-emmanuelisnardon8308 Год назад

    Great video ! Well explained ! Thank you, I'm modifying my code right now...

  • @crim-son
    @crim-son Год назад +1

    This is really helpful ❤️

  • @CarlosRenanFonsecadaRocha
    @CarlosRenanFonsecadaRocha Год назад +9

    A good thing to point out about the first mistake is that a good UX informs the user if the field has any errors while they are typing; in this case, this is not possible. Better to stick with states, but it is a nice thing to know.

  • @user-cp4wf4xy1g
    @user-cp4wf4xy1g Год назад

    Simple and brilliant

  • @sergiogusto
    @sergiogusto Год назад

    00:45 tag stores fields value

  • @jeromealtariba7339
    @jeromealtariba7339 Год назад

    excellent ! Big thanks

  • @gilsonconceicao5201
    @gilsonconceicao5201 Год назад

    Thanks. I didn't knew these.

  • @paulbird2772
    @paulbird2772 Год назад +9

    Very clear video, I just switched to Typescript for last major project, took a bit of effort initially but rewards are great, the build step catches a good few errors very early. I think not using TS could be added to your list. I expect many that haven't given it sufficient time will disagree.

    • @dannyj7262
      @dannyj7262 Год назад +3

      I spent hours on my degree final project sorting out errors that js didn't pick up until I tried to run a piece of code with an error in. Switched to TS and it's a million times better and Id also say I've learnt a lot more too

  •  Год назад

    You just prevented some lay offs, great job.

  • @FSRezende
    @FSRezende Год назад

    Great! Thanks from Brasil!!!

  • @raziechavoshi5994
    @raziechavoshi5994 Год назад

    super informative and helpful

  • @shivshankarshejwal6728
    @shivshankarshejwal6728 Год назад +2

    Nice tip last one.Thanks

  • @taulantus
    @taulantus Год назад +4

    For the first case you don’t need state or refs the onSubmit function has access to all the input fields via the event parameter

  • @beinghappy3924
    @beinghappy3924 Год назад

    You really deserve thumbs up from me. Good Job. 👍

  • @hazemalabiad
    @hazemalabiad Год назад

    You are AMAZING ... Kyle 💐

  • @carlosricardoziegler2650
    @carlosricardoziegler2650 Год назад

    Good tips , thanks for sharing

  • @sheriffawzy2998
    @sheriffawzy2998 Год назад

    Good work Kyle 😉

  • @evrentank6402
    @evrentank6402 Год назад

    Thanks a lot. These are very important and useful knowledges.

  • @habibiSD
    @habibiSD Год назад

    Well done sir, excellent video. Thank you for your hard work.

  • @sagarmishra6746
    @sagarmishra6746 Год назад

    I even express with words how much this video has helped me. I got answers to lot of my questions. Thanks Kyle💥

  • @noelfrancisco5778
    @noelfrancisco5778 Год назад

    great tutorial, I learned something new today. Thanks :)

  • @adnanhaider9302
    @adnanhaider9302 Год назад +9

    The number of useEffect gotchas and permutations are just never ending. I have such a love hate relationship with this hook.

    • @coderyan
      @coderyan Год назад +1

      You and me both

    • @stivenmolina4075
      @stivenmolina4075 28 дней назад +2

      I have a relation of pure hate with that hook

  • @mingxindong3150
    @mingxindong3150 Год назад

    that just makes so much sense!!!! thank you for bring the good code

  • @mariumbegum7325
    @mariumbegum7325 Год назад

    Really good video, very informative and explaining in a clear way.

  • @ori-kapkap
    @ori-kapkap Год назад

    It's very helpful for React beginner like me! Thank you for your videos :)

  • @komalmadamwar710
    @komalmadamwar710 Год назад +1

    this is super helpful

  • @pedrocruz8164
    @pedrocruz8164 Год назад

    some of the senior developers in a company I worked for used to not approve my PRs asking for me to not use useRef to the store any state in react component. It is nice to see someone explaining why not every state needs to be rerendering the component on every data update.

  • @yashpreetbathla4653
    @yashpreetbathla4653 Год назад +1

    Great video dude thanks :)

  • @maximpolsky2683
    @maximpolsky2683 Год назад

    Many thanks! Good luck with your channel! Greetings from Kyiv!))

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

    this video is all react people need, thanks a lot.

  • @mikeonthebox
    @mikeonthebox Год назад +11

    I'm thankful I found Svelte and don't have to deal with this complexity. Svelte makes everything so much intuitive. Like yes, there are still some special cases and implementations where you require some special syntax, but for the most part it's just writing regular JS without having to think what wrapper you need.

  • @saheedsimisaye8978
    @saheedsimisaye8978 Год назад +3

    Most valuable React tips I have learnt regarding some bad code practices I have been applying in React. Thanks for this very comprehensive video with lots of valuable information covered in just over 20mins.

  • @mustaphasalehipourenglish7387
    @mustaphasalehipourenglish7387 Год назад

    was great like always

  • @Infinizhen
    @Infinizhen Год назад +1

    Great video. I don't know what more can i say. Cheers!

  • @MrQVeeBoo
    @MrQVeeBoo Год назад

    Very good, on point, thx

  • @ozkanylmaz8046
    @ozkanylmaz8046 Год назад

    Thank youuuu very much

  • @singhsankar
    @singhsankar Год назад

    very simple and very useful!

  • @sandhilt
    @sandhilt Год назад

    Thank you for share.

  • @youssefhossam9077
    @youssefhossam9077 Год назад

    that was helpful thank you

  • @mikejuli8243
    @mikejuli8243 Год назад

    Man, using setCount( (count)=>{count + 1} ), it's the greatest way how to handle this real react problem! That's was extremely beneficial for me !!!!! Thank you!

  • @mengfandy7365
    @mengfandy7365 Год назад +1

    i love react and i love this youtube channel, hi from indonesia

  • @ode2877
    @ode2877 Год назад

    Usefull for beginner like me, thanks 🎉

  • @AllAboutDev
    @AllAboutDev Год назад

    Last tip was really important ❤

  • @abubakarrbangura9820
    @abubakarrbangura9820 Год назад

    Thanks Kyle

  • @Caspitein
    @Caspitein Год назад +46

    Interesting video Kyle! I noticed I do make a few of these mistakes myself. I think the problem is that us developers can be a bit "lazy" learning new frameworks. I understand how the useState and useEffect hooks work and I never took the time to learn about useRef and useMemo for example.

    • @coderyan
      @coderyan Год назад +4

      totally!

    • @Spyrie
      @Spyrie Год назад

      You'll use it once you handle big data and use expensive functions that can block the main thread

    • @d.ilnicki
      @d.ilnicki Год назад

      Well I this case the "lazy" one is the video author. Recommending using refs over state on 1.34M subscribers is a crime.

  • @TranNguyen-mv6ln
    @TranNguyen-mv6ln Год назад

    Thank you

  • @workwithwegs4541
    @workwithwegs4541 Год назад +3

    Hey Kyle! Thanks a lot for your awesome videos! Have you tackled about using normal function and arrow function in react js? You seem to use both but I'm not sure what your criteria is. Hope you make a video about it! Thanks!!

  • @jesuscabrita5815
    @jesuscabrita5815 Год назад

    that useMemo trick... was lovely

  • @viet.khoaiegg
    @viet.khoaiegg Год назад

    Learning so much 🎉

  • @monafdaod2133
    @monafdaod2133 Год назад

    Thanks, A lot👍

  • @swaminarayansharma1921
    @swaminarayansharma1921 Год назад

    Really helpful

  • @adirmugrabi
    @adirmugrabi Год назад

    wonderful video.

  • @it9hektar
    @it9hektar Год назад

    good explanation !!!, my problem can be solved, thank you very much..

  • @TheVisionBT
    @TheVisionBT Год назад

    thank you

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

    Thanks

  • @appuser
    @appuser Год назад

    This video rocked my world. Had quite a REACTion to it.

  • @sam-u-el
    @sam-u-el Год назад +1

    thank you sir what was informative

  • @radhouze2554
    @radhouze2554 Год назад

    Subbed for that beautiful Jackson guitar

  • @mehmetacikgoz9814
    @mehmetacikgoz9814 Год назад +333

    Using refs instead of state does not seem right in this scenario. First of all, such a render does not create any overhead since behind the scenes react will do equivalent of what you have done with refs. Besides, these code will probably be improved with validators etc. which will result going back to state version

    • @rvft
      @rvft Год назад

      Sus lan

    • @neutralface
      @neutralface Год назад +57

      You're correct. React has always preferred controlled components (react handles input value) over uncontrolled components (DOM handles input value). This is second time I see Kyle making this counter-argument to use refs, which I think is incorrect.

    • @ACMonemanband
      @ACMonemanband Год назад +9

      This is really debatable, as the scenario Kyle mentioned is that, all the inputs are only used upon form submission. For experienced React developers, we have always been using controlled components and get really used to it, so we are very unlikely to use Kyle's uncontrolled ways in any circumstance.

    • @neutralface
      @neutralface Год назад +18

      @@ACMonemanband True. Refs are mentioned in the "Escape hatches" section in the new react docs, which for me intuitively tells that refs are used as secondary option when first option of controlled components doesn't work.

    • @rand0mtv660
      @rand0mtv660 Год назад +19

      ​@@ACMonemanband These days I usually just use react-hook-form and don't worry if something is controlled or uncontrolled in a most forms. The fact is that for more complex forms that might compute a lot of stuff, uncontrolled form inputs are better because they won't re-render the whole form on each input change. If you need to do any active validation, you'll probably have to go for controlled forms because you'll have to react to form changes all the time and not just on submit, but in cases where you just need to validate on submit, you probably want to go for uncontrolled inputs.
      Only reason I dislike his input ref example is because you'll rarely only work with two field forms in the real world and you'll most likely have multiple (more complex) forms in your app. At that point, just reach for something like react-hook-form and never worry about building forms in React.

  • @monara97
    @monara97 Год назад

    Awesome!!

  • @nirmesh44
    @nirmesh44 Год назад

    best tips ever. i also was not using functional way of setting state

  • @g_sami__newn8728
    @g_sami__newn8728 Год назад

    Useful video❤️

  • @User36282
    @User36282 Год назад +3

    You really have a talent for education my man. Whenever I'm not understanding something in web dev now I just search for web dev simplified and boom there's always a video on it. Thank you! These were really good tips for those of us just getting into front end :)

  • @vivekkaushik9508
    @vivekkaushik9508 Год назад

    Wonderful.

  • @iamsajjadalidev
    @iamsajjadalidev Год назад

    thanks

  • @djedankharkryonia4223
    @djedankharkryonia4223 Год назад

    Awesome!

  • @AR7editing
    @AR7editing Год назад +9

    15:45 in that case instead of useMemo we can directly use in the dependency array person.name and person.age and it will not cause rendering when you change the dark mode checkbox

  • @KathidFX
    @KathidFX Год назад

    It does work as the same as before in case of email and password, but we use usestate , value and onchange to create Controlled components. Uncontrolled components with refs are fine if your form is incredibly simple regarding UI feedback. However, controlled input fields are the way to go if you need more features in your forms.

  • @carlesgalino3213
    @carlesgalino3213 Год назад

    so helpful