React Hooks useCallback Tutorial

Поділитися
Вставка
  • Опубліковано 4 лис 2024

КОМЕНТАРІ • 111

  • @aleksd286
    @aleksd286 5 років тому +66

    The best React videos on UA-cam to be honest, I've learned so much from these vids, and I can see that my code is getting only better

  • @abdoumenouer7762
    @abdoumenouer7762 3 роки тому +9

    I just like how the numbers you chose align perfectly with the timestamp 7:21

    • @frontendHacks
      @frontendHacks 2 роки тому

      watch useCallback Hook in Hindi in easy way explained
      ua-cam.com/video/Gj68rN0vLSc/v-deo.html&ab_channel=Front-EndHacks

  • @markod3957
    @markod3957 4 роки тому +7

    Dude these hook tutorials are brilliant, and genuinely the best I have come across in my the time I have used React - and I have explored a fair amount online

  • @yoyoliyang
    @yoyoliyang 4 роки тому

    这是关于”wrap the 'fetchBusinesses' definition into its own useCallback() Hook“很好的解决方法和教程,感谢!
    This is a good tutorial about "wrap the 'fetchBusinesses' definition into its own useCallback() Hook" question. thanks!

  • @louicoder
    @louicoder 4 роки тому +1

    Yo @Ben Awad , you're such a blessing. here in 2020 looking for ways to optimize my code and couldn't find any better explanation than this. Thanks so much for your content. Always grateful , God bless you . Mad love from Uganda [ East Africa ]. 🚀🎉🎉

  • @Robert-cw9ev
    @Robert-cw9ev 4 роки тому +2

    Thank you so much!
    I used this for interaction with Canvas, because it was updating even if it didn't depend on any state variable.

  • @eat_mangos4177
    @eat_mangos4177 5 років тому +9

    this is as good as it gets with youtube tutorials, cheers

  • @EricBishard
    @EricBishard 5 років тому +4

    It would be nice to have a series of Hooks videos that build up a small application that utilizes useMemo, useCallback and creating custom hooks from a fundamental first perspective, why these methods will help you create more performant apps and components and a little bit slower of a pace.
    I understood this video, but I had to watch it a few times and I'm still trying to think of good practical examples where this info can help me out. Just an idea! This video really helped me though...

    • @bawad
      @bawad  5 років тому +1

      that's a good idea

  • @chisomoguibe8467
    @chisomoguibe8467 4 роки тому +9

    That swallow at the beginning tho 😂

  • @LeonardoXavier1
    @LeonardoXavier1 5 років тому +4

    Thanks a lot, Ben. This series is really helping me to better understand the hooks and I`m watching each video carefully.
    In case of this "useCallback" hook, even with you explanation, I can`t figure out when I have to memoize, how to properly do it and how to manage rerenders. I didn't even understand how can your component doesn't rerender and yet shows the incremented number on the screen. So, can you go back, maybe one extra video outside this series, and, from the begining, explain this "memo" and "useCallback" concepts?
    Anyway I'm already greatefull for the job you are doing here. It's really helpfull.

    • @bawad
      @bawad  5 років тому +3

      This video should help explain: ua-cam.com/video/o-alRbk_zP0/v-deo.html

    • @locky326
      @locky326 5 років тому +4

      I think you missed the fact that the component showing incremented value is the parent of the buttons - so the buttons stay the same and they only render once. However, without memo the buttons would re-render every time the incremented value changed because the component displaying that value re-renders.

    • @LeonardoXavier1
      @LeonardoXavier1 5 років тому

      @@locky326 yep, I really missed It. Thanks.

    • @LeonardoXavier1
      @LeonardoXavier1 5 років тому +1

      ​@@bawad This was great. I could get everything now. Thanks again, man!! You are my super hero.

  • @vileider
    @vileider 2 роки тому +1

    Ben you supposed to explain how to use useCallback, and You assumed that I already know custom hooks and useMemo.

  • @Seb16291629
    @Seb16291629 5 років тому

    Very useful thanks. Very hard to not get confused between all different hooks . In my app, I m using use effect most of the time, something isn’t probably right.

  • @austinlords582
    @austinlords582 4 роки тому

    Brilliant tutorial. Perfect length and depth of explanation.

  • @yuxingzhou4844
    @yuxingzhou4844 2 роки тому

    Best explanation ever.

  • @elciesstv
    @elciesstv 4 роки тому

    Love the way u explain, so clean and easy to understand. TY!

  • @itsmattchan
    @itsmattchan 4 роки тому

    Am I the only one that's amazed every video by Ben's keyboard shortcuts?

    • @bawad
      @bawad  4 роки тому

      You should start doing them too
      benawad.com/vscode
      benawad.com/vim

  • @mcnimi
    @mcnimi 4 роки тому

    probably the most helpful video i've seen on react (also native). thank you.

  • @arpielen
    @arpielen 3 роки тому +1

    Wish I could have a little Ben in my pocket to carry everywhere I go and he would teach me things when I need 🥴🥴

    • @mikealejandro3938
      @mikealejandro3938 3 роки тому

      Lmao that sounds creepy, but i think ben would like that idea

    • @arpielen
      @arpielen 3 роки тому +1

      @@mikealejandro3938 Well, now that you said... It really seems weird LOL 😂

  • @TheElijbet
    @TheElijbet 4 роки тому +1

    Not sure how you eliminated count dependency by using count => count + 1 instead?

  • @teytag8844
    @teytag8844 5 років тому +5

    Sooo useful, thanks for the explanation

  • @equality9304
    @equality9304 4 роки тому +1

    I have learned a lot from this video.

  • @Twistee
    @Twistee 4 роки тому

    Did not know you could get rid of state (count) like that in useEffect, that's going to be super helpful!

    • @frontendHacks
      @frontendHacks 2 роки тому

      watch useCallback Hook in Hindi in easy way explained
      ua-cam.com/video/Gj68rN0vLSc/v-deo.html&ab_channel=Front-EndHacks

  • @xerxius5446
    @xerxius5446 4 роки тому

    Awesome Tutorial! Thanks for your work. Much appreciated

  • @darryl_young
    @darryl_young 5 років тому +2

    Thanks, Ben. Another great video.

  • @debasishsahapranta6247
    @debasishsahapranta6247 4 роки тому

    Why we need to add 'setCount' as a dependency of "useCallback", putting the dependency array blank should work fine. Is there any case where the "setCount" function can be changed?

  • @sariksiddiqui6059
    @sariksiddiqui6059 4 роки тому +1

    Doesn't using function instead of lambda also recreate functions on each render?

  • @iluha005
    @iluha005 4 роки тому

    Nice work! Thank you, Ben.

  • @babynini1513
    @babynini1513 4 роки тому +2

    Thanks for good examples! Can i ask what's the reason to use [setCount] as dependency rather than just [ ] since we don't want it to re-render?

    • @lenientliu6080
      @lenientliu6080 2 роки тому

      The setCount will not be changed in each render after created.

  • @drewstifler1440
    @drewstifler1440 4 роки тому

    What's the difference if you put the updater function as the dependency and just leaving it as an empty array? Both works.

  • @RusuTraianCristian
    @RusuTraianCristian 4 роки тому

    Brilliant, loved it!

  • @agustinernestocardeilhacba1765
    @agustinernestocardeilhacba1765 4 роки тому

    Thanks Ben !!! :) Really clearly explained ! :D

  • @abdoumenouer7762
    @abdoumenouer7762 3 роки тому

    Great video! thanks!

  • @pulga961
    @pulga961 4 роки тому +1

    Wait, we can just pass reference to increment function and shallow equal will return true.
    const increment =>(){......}
    and then pass via props increment={increment}. Ben?We can avoid using useCallback?

    • @bawad
      @bawad  4 роки тому +1

      useCallback is how you keep the reference the same

  • @zag2art
    @zag2art 4 роки тому +1

    Great man, thank you! It helps me a lot

  • @collinsk8754
    @collinsk8754 Рік тому

    Really useful.

  • @sachinbhandari345
    @sachinbhandari345 3 роки тому

    Why setCount is in the second parameter of useCallback. Does react changes its reference on every render?

  • @kenhilliard5845
    @kenhilliard5845 5 років тому +1

    For useCallback you first use [count, setCount] as its dependencies. Later, at around 5:20, you remove "count" as a dependency for useCallback, leaving only [setCount]. You then changed the setCount to the updater style: setCount(c => c + 1). Why was this necessary? Why not just remove "count" as a dependency and keep setCount(count + 1)?

    • @bawad
      @bawad  5 років тому

      Try removing count as a dependency and keep it in there, you'll see wonky results.
      it has to do with closures: ua-cam.com/video/eTDnfS2_WE4/v-deo.html

    • @sagaruv
      @sagaruv 4 роки тому

      If you use setCount(count + 1) inside useCallback and remove count from dependencies array, the value of the count will always be its initial value (ie 0)

  • @thomaschen2057
    @thomaschen2057 4 роки тому

    Something really helpful not just 'to do list' tutorials

  • @filmologyaz4729
    @filmologyaz4729 4 роки тому

    thank you Ben

  • @alexandrudanpop
    @alexandrudanpop 5 років тому +1

    Thanks Ben!

  • @edgarcatalan6462
    @edgarcatalan6462 2 роки тому

    I forgot what is the difference with setCount(count + 1) and setCount(c => c + 1) ?

  • @les2997
    @les2997 5 років тому +7

    At 5:32, why there is a dependency on setCount? setCount is already cached and it will not change.

    • @bawad
      @bawad  5 років тому +2

      Because if setCount were to change I want the new value

    • @les2997
      @les2997 5 років тому +6

      @@bawad Yes, but setCount is cached and it will not change.
      github.com/jamiebuilds/unstated-next/issues/43

  • @ursochurrasqueira
    @ursochurrasqueira 4 роки тому

    great video, helped me a lot

  • @usamaimran1516
    @usamaimran1516 3 роки тому

    What about data fetching? Lets say i am fetching data in use effect how can i optimize that? So that if fetched data is same, it doesnt rerender that component

  • @VinitKhandelwal
    @VinitKhandelwal 4 роки тому

    Does it compares the old value and new value shallowly or deeply?

  • @iamdihan
    @iamdihan 4 роки тому

    This is great! How did i now know this

  • @Marketblank
    @Marketblank 3 роки тому

    Hi , I tried to do same thing to useForm but not sure why it still doing re-render . i can share the changes

  • @kacperkedzierski9047
    @kacperkedzierski9047 4 роки тому +1

    I don't understand why there's a [setCount] dependency in callback, couldn't it have been just empty [ ]? When will setCount ever change?

    • @TomWacaster
      @TomWacaster 4 роки тому +2

      useCallback doesn't have any special knowledge of what setCount is. We know that it's a setter from useState, but the code inside useCallback only knows that it is a function. Since functions can be recreated (for example if it was a function that had been defined by another useCallback), we need to know to execute this useCallback again if "setCount" changed definition.

    • @kacperkedzierski9047
      @kacperkedzierski9047 4 роки тому +1

      @@TomWacaster Oh, true I didn't even think of that. Thanks!

    • @debasishsahapranta6247
      @debasishsahapranta6247 4 роки тому

      @@TomWacaster but in this case, we can set the dependency array blank as we are never going to change the setCount function, right?

  • @ashshkapoor
    @ashshkapoor 4 роки тому +1

    Just completed vimtutor and I already feel like giving up after watching your vim skills.

  • @joerivde
    @joerivde 4 роки тому

    Subbed, this is gold

    • @bawad
      @bawad  4 роки тому

      welcome :)

  • @adamjohnston3157
    @adamjohnston3157 5 років тому

    On both of the examples, are you not able to just pass setState to the child component as it is guaranteed to never change? I have a hard time seeing any use cases for useCallback unless the function itself did something computationally expensive. Still a great explanation regardless.

    • @bawad
      @bawad  5 років тому +1

      you can pass setState down, but sometimes you want to create a function that you share across multiple components and useCallback is handy there

  • @mokkamokka4097
    @mokkamokka4097 5 років тому +1

    Thank you so much!

  • @antoniosousa4178
    @antoniosousa4178 3 роки тому

    Ben, how to you change cursos type?

  • @markkkly810
    @markkkly810 4 роки тому

    Amazing

  • @hoangnt142
    @hoangnt142 4 роки тому

    Thank you!

  • @maynardewm
    @maynardewm 4 роки тому +17

    This made zero sense to me

  • @Gbd279
    @Gbd279 4 роки тому

    Why doesn't react memoize renders by default?

    • @bawad
      @bawad  4 роки тому +1

      Because you generally don't need it

  • @ManojSingh-of5ep
    @ManojSingh-of5ep 4 роки тому +1

    why my components are rendering twice

    • @debasishsahapranta6247
      @debasishsahapranta6247 4 роки тому +6

      Maybe your app is wrapped with "React.StrictMode" on index.js. Try removing this.

  • @reallegend1
    @reallegend1 4 роки тому

    Thanks Ben , I hope that you can find a way to remove the keyboard voice from the video.

  • @andiboggs4902
    @andiboggs4902 2 роки тому

    When i try this tutorial in 2022 the count increments like 0,1,3,5,7 ...

    • @andiboggs4902
      @andiboggs4902 2 роки тому

      in the console but increments normally in the browser view.

  • @peoplearecool8766
    @peoplearecool8766 4 роки тому

    Guys I have one question if someone can help me please , so if component is number or string or array or object, Ract.memo is working - that means component will not render if those (data types ) props are same as before. but why does component renders if one of the property is function : just like in this video , if we write even so icrement function is not changing Hello component steel renders every time and we need to use useCallback hook in that case .
    So my final question is why does react.memo not working with function properties even so this functions stay same , or do they change in every render ? If function change every time why does people say that " increment={ increment } " is better practise then " increment={ () => setCounter(...) } " - this inline anonymous functions because anonymous functions are created every time when component renders ... I am little confused :D :D

  • @alex_chugaev
    @alex_chugaev 3 роки тому +1

    Bad news: the function you pass as the first arg to "useCallback" will still be created on each render 😂

    • @bastek338
      @bastek338 3 роки тому +1

      Yup, optimizing apps with React is not the easiest thing

    • @inordirectional
      @inordirectional 3 роки тому

      The overhead of recreating the function wasn't what he was trying to avoid as much as re-rendering a potentially large child component to which that function is passed as a prop. That's what he was showing how to avoid

  • @alex_chugaev
    @alex_chugaev 3 роки тому +1

    React is so badly designed you have to optimize every bit of it yourself

  • @Levelord92
    @Levelord92 3 роки тому +1

    I didn't get it at all :)

  • @cleverocheckts518
    @cleverocheckts518 4 роки тому

    Я не понимаю что ты говоришь.Ну а код нормальный,и хорошо объясняет

  • @okoiful
    @okoiful 4 роки тому

    why in the heck do we have to be jumping through hoops like this in react? we shouldnt have to be doinf this.

  • @1ycx
    @1ycx 5 років тому

    8 mins ago. 8 views.

  • @rafaelso9991
    @rafaelso9991 4 роки тому

    I have no idea what hes saying. its not good for newbie like me haha