Learn React Hooks: useCallback - Simply Explained!

Sdílet
Vložit
  • čas přidán 13. 05. 2023
  • Join The Discord! → discord.cosdensolutions.io
    Source Code → github.com/cosdensolutions/co...
    In this video we will learn about React hooks, starting with useCallback. This powerful React hook will allow you to fix your performance problems by memoizing a function and only re-computing it when necessary. You will learn how to identify performance problems from functions causing sub-components to re-render, how to memoize the functions using React's useCallback hook, and how to use the dependency array to control exactly when and how your function updates.
    In this new React world, hooks are here to stay, so it's best to learn them! In this tutorial I demonstrate the useCallback React hook, and I explain it very simply and in a way that is easy to understand. Enjoy!

Komentáře • 191

  • @cosdensolutions
    @cosdensolutions  Před 2 měsíci +3

    Hey everyone! I just launched 🚀 Project React, which is a course that teaches you React by building a real-world project. It goes way beyond what you see in these videos and walks you through step-by-step on how to build a big and complex application with React! You can check it out here: cosden.solutions/project-react

  • @heybran_
    @heybran_ Před rokem +203

    Best react educator found on youtube! Agreed everyone?

  • @ahmadbenchakhtir5787
    @ahmadbenchakhtir5787 Před 18 dny +2

    The truth is: i watched two tutorials about UseCallback from great people talking my native language -Arabic-, but i didn't get the whole idea properly. now i do. Thank u.

  • @lexsemenenko7044
    @lexsemenenko7044 Před 10 měsíci +22

    Clean and clear explanation with examples on what and why for us to clarify the ideas behind the hook. Again, nailed! Appreciated.

  • @SWE507
    @SWE507 Před 2 měsíci +1

    This is the cleanest and clearest explanation of useCallback ever!!!

  • @CalltheWei1020
    @CalltheWei1020 Před 7 měsíci

    Love how you explain the whys and what really happens behind the scenes. Thanks and much appreciated

  • @JakeLuden
    @JakeLuden Před 10 měsíci +13

    Genuinely the best useCallback tutorial I’ve seen, probably the best video I’ve seen regarding hooks in general. Incredible explanation and demonstrations!

    • @cosdensolutions
      @cosdensolutions  Před 10 měsíci +1

      damn, thanks for saying that! Really means a lot 🤙

    • @atomvandermerwe3613
      @atomvandermerwe3613 Před 10 měsíci

      I 100% agree with @Jakezaruba, this video explained it perfectly.

  • @lagekutsa
    @lagekutsa Před 3 měsíci +2

    you explain useCallback so simple man, really appreciate.

  • @whiterabbitdev
    @whiterabbitdev Před 6 měsíci

    cheers dude, none of that "learn this in 3 minutes bs" you explain everything nice and clearly!

  • @tingwen1713
    @tingwen1713 Před 3 měsíci

    Hey! Just wanted to say that I've been going through your Learn React Hooks playlist and it's been so helpful. Thank you for making this content. You're a great educator!

  • @smokinjoe3000
    @smokinjoe3000 Před 4 měsíci +1

    Just discovered these today - and they're so well presented! These are things I use at work every day and you've done such a great job in explaining the benefits and pitfalls of each of these react hooks! Definitely pointing any new engineer over to your channel to learn/understand them

  • @user-be3ri8gf1p
    @user-be3ri8gf1p Před měsícem +1

    Best hooks tutorial I've encountered so far! Thanks 🔥

  • @gmjitendra
    @gmjitendra Před 8 měsíci

    you are very clear concept wise. No need to follow any other videos on react hooks. really liked all your videos. Thanks and much appreciated

  • @rajeshmaydev
    @rajeshmaydev Před 2 měsíci

    the words you picked "freeze in time". Really helped me wrap the idea of it after many months of doubt

  • @chengxiaoxia8046
    @chengxiaoxia8046 Před 2 měsíci +2

    You are a serious teacher. You tested your code truly before publishing this video, I think. I've read many articles about useCallback and their code did not work correctly coz they did not use 'memo'. Thanks so much. Your video brings us true value.

    • @learner8084
      @learner8084 Před 2 měsíci

      I didn't know about the memo either ! Thanks very much.

  • @shamimhossain551
    @shamimhossain551 Před 6 měsíci

    Ahh! you make my day. Was in a maze whole day about the using of useCallback. You've just come up with a propper salvation. Thanks ❤

  • @manojmrpd
    @manojmrpd Před 20 dny

    You are really a great educator. The best react tutorials found on CZcams! Very Appreciated @Cosden Solutions

  • @RaGa_BABA
    @RaGa_BABA Před měsícem +1

    Just a small add @2:30, its not that in react functions are different, its that they are referenced type in js, very well explained, really helpful thankyou

  • @noahmilliken4259
    @noahmilliken4259 Před 3 měsíci

    Props to Cosden for not bombarding us with some 15 second intro branding. Down to business, love it.

  • @__shubhamtiwari
    @__shubhamtiwari Před 4 měsíci

    Bhai sahab matalab ekdam gajab padhaya yaar, dil jeet liya dost ❤

  • @shubham.verma-dev
    @shubham.verma-dev Před 10 měsíci +3

    Nicely explained. I tried to understand from other videos as well, but this is really good. Now I am done with my doubts.

  • @vivekkumar-pc1xy
    @vivekkumar-pc1xy Před 6 měsíci

    Great video, well explained. Everything is now crystal clear

  • @oscardasilva971
    @oscardasilva971 Před rokem

    The last example was very clever. Thank you sir.

  • @fhkodama
    @fhkodama Před 4 měsíci

    Oh, finally a good explanation about this hook! You got a subscriber with this video! Thanks!

  • @ProfessionalClickers
    @ProfessionalClickers Před 9 měsíci

    Finally I understood useCallback ❤️ Thanks a lot!

  • @uquantum
    @uquantum Před 8 měsíci +4

    Great video for React devs at any level. I'd forgotten that a React component that takes in a function from its parent will re-render even if the variables in that function haven't changed. Not obvious until you explained it so well. You also made it super simple and clear that useCallback memoizes a function like useMemo memoizes a variable, and that helps us minimize unnecessary re-renders.Thanks!

  • @catalincatalin4101
    @catalincatalin4101 Před 7 měsíci

    We really needed someone make this hooks simple. Thanks

  • @kisuhara7844
    @kisuhara7844 Před 4 měsíci

    literally the best react tutor thank you for your videos

  • @vesa95
    @vesa95 Před 11 měsíci +1

    Awesome explanation, man! very, very clever!

  • @radhiarahmani9523
    @radhiarahmani9523 Před 26 dny

    Very well explained! Thank you so much

  • @borameupai1792
    @borameupai1792 Před 2 měsíci

    Your explain saved my life. U just got a new subscriber. Thanks.

  • @coolme7437
    @coolme7437 Před 4 měsíci

    Thanks, this really is my last useCallback tutorial.

  • @VijayVibhuteFlautist
    @VijayVibhuteFlautist Před 4 měsíci

    Love it! Appreciate the way you explained everything. ❤

  • @aleksandrgolyk3787
    @aleksandrgolyk3787 Před 3 měsíci

    Great explanation 🎉🎉🎉 i was so struggling with understanding this topic. Thank you🎉🎉🎉

  • @Gangbuster74
    @Gangbuster74 Před 11 měsíci +1

    By far this is rhe best explanation have watched many many tutorials, but you explained it in very simple terms 🎉ty

  • @viliuxsas
    @viliuxsas Před 7 měsíci

    I am happy that I found this channel! Thank you for very clearly explanation about React hooks and because of you I improved my knowledge.

  • @ASOCMARCTKD
    @ASOCMARCTKD Před měsícem

    Excellent explanation, thank you 😇

  • @antimrevapati8992
    @antimrevapati8992 Před 3 měsíci

    Thank you so much for making these easy to understand videos. The most important thing you did was first intruduce the problem and then explain what the topic is and how can we solve this issue using this topic, this is a wonderful strategy to follow. Thanks again!

  • @ahmetzeren6878
    @ahmetzeren6878 Před 6 měsíci

    great explanation thank you. no blank spaces left

  • @HenryBabbage
    @HenryBabbage Před 10 měsíci

    Awesome explanation. Thanks!

  • @dilshadazam880
    @dilshadazam880 Před 7 měsíci

    You deserve atleast 1 million subscribers. Please keep making such videos

  • @Jam....
    @Jam.... Před 6 měsíci

    Excellent clear explanation thank you.

  • @PradeepManek
    @PradeepManek Před měsícem

    Thank you brother you explain this topic very well.

  • @belkocik
    @belkocik Před rokem

    Very clear explanation!

  • @attouyassine1056
    @attouyassine1056 Před 3 měsíci

    Appreciate the way you explained it, thanks

  • @peirama2
    @peirama2 Před 5 měsíci

    Man, i follow and watch the videos of many tech youtubers and i have seen a ton of tutorials from many of them. This single video is the best tutorial of any concept i have ever seen. Tons of respect!

    • @cosdensolutions
      @cosdensolutions  Před 5 měsíci

      Thank you for the kind words!

    • @peirama2
      @peirama2 Před 5 měsíci

      @@cosdensolutions thank you for the many “aha” moments

  • @WaseemAhmad-mf3wh
    @WaseemAhmad-mf3wh Před 2 měsíci

    Thank you for your clear explanation ❤

  • @Za-xh9tj
    @Za-xh9tj Před 3 měsíci

    Expected to learn useCallback, instead I learned how react rerender, memo, and usecallback works. Amazing.😄

  • @cat_demon4405
    @cat_demon4405 Před 9 měsíci

    Underrated channel! subscribed

  • @saikumar.reach99
    @saikumar.reach99 Před 2 dny

    Helped me understand callbacks it was tough earlier

  • @pythonbrothersandfamily
    @pythonbrothersandfamily Před měsícem

    omggg Great explanation. Thank you bro

  • @briandacallos4234
    @briandacallos4234 Před 9 měsíci

    I'm shocked how he explained it very well than anyone could. Automatic subs!

  • @dylcodes
    @dylcodes Před měsícem

    Great video, thank you!

  • @alangraton2000
    @alangraton2000 Před 7 měsíci

    Dude, your channel is the bomb. Thanks for the vids ^-^

  • @SPribyt
    @SPribyt Před 9 měsíci

    Good job explaining!

  • @legend_749
    @legend_749 Před 9 měsíci

    awesome explaination, pls continue this series

  • @2029leandro
    @2029leandro Před 3 měsíci

    Perfectly explained!

  • @diggerdog001
    @diggerdog001 Před 5 měsíci

    15:32 When u put 'users' in arr dep i really thought them why we need use Callback if we started from where our problem starts. And you began explain 😀

  • @rgchroniclestv
    @rgchroniclestv Před 9 měsíci

    Really a big help. Thanks

  • @shabeeeb6400
    @shabeeeb6400 Před 7 měsíci

    you earned a sub :) great explanation

  • @MudassirKhan-sx9jy
    @MudassirKhan-sx9jy Před 8 měsíci

    so clear. thank you ♥

  • @abhinavdhama3014
    @abhinavdhama3014 Před 8 měsíci

    Amazing explanation on hooks ❤❤❤❤

  • @aliksayfullin3278
    @aliksayfullin3278 Před 3 měsíci

    Great explanation 👍

  • @OXIDE777-is6gs
    @OXIDE777-is6gs Před rokem

    Awesome man! thanks a bunch!

  • @samahgad241
    @samahgad241 Před 5 měsíci

    thanks alot, very happy to know your channel🌹

  • @sidkan9251
    @sidkan9251 Před 8 měsíci

    you got a new subscriber
    thanks man

  • @umairiqbal1483
    @umairiqbal1483 Před 8 měsíci

    Thanks. It really helped.

  • @sukritsaha5632
    @sukritsaha5632 Před rokem

    Great explanation

  • @tigranharutyunyan7674
    @tigranharutyunyan7674 Před 5 měsíci

    Enjoyed very much

  • @maryannegichohi7063
    @maryannegichohi7063 Před 4 měsíci

    Amazing tutorial

  • @haiderjaafer8164
    @haiderjaafer8164 Před rokem

    Great to see you here I followed you on tiktok. So great work keep going forward

  • @zul.overflow
    @zul.overflow Před 3 měsíci

    clean and clear 👍

  • @remix_me
    @remix_me Před rokem

    you earned a new subscriber

  • @TheMakeupmonika
    @TheMakeupmonika Před 10 měsíci

    love you man ! thank you so much

  • @atakan8570
    @atakan8570 Před 7 měsíci

    best teacher ever

  • @mralextacy
    @mralextacy Před 7 měsíci

    awesome tutorial

  • @user-dq1to7hg2n
    @user-dq1to7hg2n Před 28 dny

    Excellent!

  • @madehayoussof
    @madehayoussof Před 2 měsíci

    Amazing! thanks

  • @SahilKashyap64
    @SahilKashyap64 Před 9 dny

    Thank you for this

  • @mostinho7
    @mostinho7 Před 3 měsíci +1

    Done thanks clear explanation

  • @nayandey5010
    @nayandey5010 Před 6 měsíci +1

    Great explanations sir the only concern is I watched 5 ads each containing 2 ads(which I can't skip) 😂😂😂

  • @nyomansunima
    @nyomansunima Před 8 měsíci

    Good explaination. Now i know it

  • @raymondmichael4987
    @raymondmichael4987 Před 11 měsíci

    Thanks buddy 😊😊

  • @mobinhamidi4161
    @mobinhamidi4161 Před 2 měsíci

    Thanks ❤

  • @ahallock
    @ahallock Před 4 měsíci

    Thanks for this clear explanation. As a React beginner, I feel like the DX is pretty terrible. You have to litter your code with all these hooks and remember what's changing and where. I worked with Bacon.js before and the mental model is much better, in my opinion, for handling state changes over time. React feels like spaghetti to me.

  • @INetreba
    @INetreba Před 2 měsíci

    brilliant

  • @Akhillbj
    @Akhillbj Před 11 měsíci

    When you say, onChange prop is different from the previous do you mean like actual props of the searchProps or values of the existing props?

    • @cosdensolutions
      @cosdensolutions  Před 11 měsíci

      the `onChange` function, although it's the same function that does the same code (values don't really matter here), it'll be considered different because it'll be a different instance of the function. Different instance means it's just a copy of the function, but it's not the same one in memory. So even though it looks the same and does the same thing, it is a separate entity, thus it will cause the props to evaluate as not equal

  • @smchap9474
    @smchap9474 Před 6 měsíci

    To understand, with this particular example we did actually undo all the purpose of useCallvack since users will be different when shuffled, right?

  • @talidamg
    @talidamg Před 4 dny

    Thank you for the clear explanation, Darius. Can you maybe share the shuffle function with us? It's not in the source code. :D

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt Před 5 měsíci

    Awesome

  • @bismarckkaine2700
    @bismarckkaine2700 Před 8 měsíci

    You're awesome

  • @cordial
    @cordial Před 7 měsíci +1

    After you add 'users' as a dependency to the useCallback, if you press shuffle, would the search component still not rerender?

    • @cosdensolutions
      @cosdensolutions  Před 7 měsíci +1

      Yes it does, but that is what you want. You don't want to prevent a render, you just want to control when it happens. So if there was another piece of state that would change, then it wouldn't re-render. You only want to re-render when what it depends on changes, and nothing else!

    • @cordial
      @cordial Před 7 měsíci

      @@cosdensolutions cheers. great video as always btw.

  • @nitinmali7430
    @nitinmali7430 Před 4 měsíci

    Thanks Cosden. One question. What's the difference example onChange={handleSearch} and onChange={()=>handleSearch()} ?

    • @cosdensolutions
      @cosdensolutions  Před 4 měsíci +1

      they're the same, but the second way you'll have to pass all of the arguments manually, if you have some. If you pass it like the first way, they'll get automatically passed

  • @iamparmjeetmishra
    @iamparmjeetmishra Před měsícem

    thanks

  • @giannizamora7247
    @giannizamora7247 Před 3 měsíci

    One of the things I noticed at 15:05 is that 'alex' shows but If you typed "james" the console log users[0] would have still been alex and that's because before our input changed alex was the first person in that list.

  • @satyamkumar6469
    @satyamkumar6469 Před 4 měsíci

    I appreciate the effort you put to make this kind of understanding.❤👏
    But i am confused at the last point when you added a dependency users to useCallback function which solves the search problem but won't this disturb the shuffle function also coz this is also updating the same users right?
    Which eventually created the probelm what we have seen at the first
    If am i wrong please correct me

    • @cosdensolutions
      @cosdensolutions  Před 4 měsíci

      yes, but the goal is not to prevent re-renders, but rather to control them. If you add users, then you have to re-calculate everytime it changes. That's ok. However, if you add a new state variable and change that instead, then the useMemo will not run again. That's what we want, control

  • @joshikroshan5584
    @joshikroshan5584 Před 2 měsíci

    Should we include event object in dependency array when we memoize event handlers? As it changes each time and we are using it in funciton body?

    • @cosdensolutions
      @cosdensolutions  Před 2 měsíci +1

      nope, only what is created in a component and is used in the body needs to go there. Event objects are not created in any component.Also, even if you wanted to, you couldn't. You don't have access to the event object to put it there :D

    • @joshikroshan5584
      @joshikroshan5584 Před 2 měsíci

      @@cosdensolutions thank you

  • @harag9
    @harag9 Před 7 měsíci +1

    I've watched a few useCallback vids now and started to understand it more and this has also helped a lot, thanks. However I was concerned near the end, because you put the dependency [users] on the call back the search was rendering all the time, yet the search doesn't care about the users, but if that search was an expensive component, why would you want it rendering all the time again?
    I understand the vid was mainly about the useCallback, but how would you now fix the end result to not update the search component every time... Sounds like a catch 22 situation. fix one thing, break another so to speak.

    • @ThepaytimeHD
      @ThepaytimeHD Před 6 měsíci

      This is an excellent point, I am also interested in finding a solution that solves this problem.

  • @AntNZ
    @AntNZ Před rokem +1

    Please do a useReducer tutorial 👏👏👏👏🙌🙌🙌🙌🙏🙏🙏🙏

  • @reactdev2838
    @reactdev2838 Před 6 měsíci +1

    But there will be still an issue at the last i.e. onClick shuffle, It will still re-render the Search Component again because the user state will be updated. But all over, I really appreciate it because you're teaching with core concepts.

    • @cosdensolutions
      @cosdensolutions  Před 6 měsíci +3

      The goal with this is not to prevent a re-render, but control when it happens. I mention it in the video. If user is a dependency, there's no way around it. The goal is to not have anything else cause a re-render through useCallback, which we did!

    • @reactdev2838
      @reactdev2838 Před 6 měsíci

      @@cosdensolutions Understand, But I really like your video, one of the best tutorials I have ever seen...