React Hooks Tutorial - 26 - useCallback Hook

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

КОМЕНТАРІ • 215

  • @lukemuga434
    @lukemuga434 4 роки тому +190

    The way you break down complex concepts into clear and concise explanations that are very easy to understand is just amazing. Keep up the good work

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

    “The question you might have is …” satisfies me every time. Very thoughtful videos.

  • @coder_monkey
    @coder_monkey 4 роки тому +10

    Dude, this one is super cool. The way you explain the stuff is far better. I wish you could have written the React docs.

    • @Jason-uv5tm
      @Jason-uv5tm 3 роки тому +1

      Honestly, though there is nothing wrong with react docs because if you are learning react correctly, you should know what a dependency list is (from useEffect), and if you know javascript, then you should know what callbacks are. Only my opinion though, you do you.

  • @craft99
    @craft99 4 роки тому +3

    Breaking down that sentence was incredibly helpful. Thank you.

  • @jsnjocsin
    @jsnjocsin 3 роки тому +8

    I've been starting learning react just this week and this video helps me understand one of the fundamentals of React in a clear and understandable way. Kudos to the author of this tutorial, keep up the good work!

  • @kaveenhyacinth
    @kaveenhyacinth 3 роки тому +4

    I was seeking an extensive explanation for useCallBack hook. I found it today! You are amazing bro 💙

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

    the best version of explaining useMemo & useCallback Hook!

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

    Concise and actionable. This here is an textbook example for how tech tutorials should be.

  • @pannihto7588
    @pannihto7588 4 роки тому +5

    The best explanation I've seen so far. Thank you

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

    Have been having problem for a long time understanding the concept before this very video.
    The simplest explanation. thanks for the great video

  • @nickstaresinic9933
    @nickstaresinic9933 4 роки тому +3

    All videos in this series are very well organized and presented. The explanation in *this* particular video is the clearest I've come across on using Memo & useCallback to prevent unnecessary re-renders. Thanks.

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

    Learned React easily with your concepts; you saved lot of hours to invest learning topics.

  • @singleren
    @singleren 4 роки тому +3

    The best explaination on useCallback so far

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

    Great videos. Please do a component test series after hooks!

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

    Great explanation of Memo and useCallback! Very thorough and clear.

  • @Mohamed-M-M
    @Mohamed-M-M 4 роки тому +3

    Very informative & well explained in detail, Your way of teaching is amazing, thank you.

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

    The way you explain the use cases and why and in which condition we must use certain things is just amazing. It's easy to learn concepts from you.

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

    You can optimize it more by passing the updater function as the dependency
    const incrementAge = useCallback(() => {
    setAge(age=> age + 1)
    }, [setAge])
    Result: Rendering Age
    PS: it also work without dependency

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

      Can you point out to the source from where you read about it.

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

      @@abhijeetmaurya7355 react official docs usecallback.

    • @asananddevsingh
      @asananddevsingh 3 роки тому +3

      Even no need to pass "setAge" as a dependency, because it is not the argument of the inner callback function.
      const incrementAge = React.useCallback(() => {
      setAge((prevAge) => prevAge + 1);
      }, []);

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

      @@asananddevsingh i already said that

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

      @@drewstifler1440 Yeah, You are Right.

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

    the best simplified content you can see for useCallback hook ! 👋

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

    its worth it to watch your videos after 3 yrs of uploaded concept are clear

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

    You are an amazing tutor. The way you explain the concepts are just amazing. Really helpful.

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

    excellent explanation, new function is created each rerender, and to solve that function should be memorized using a callback hook👏

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

    Thank you for making this hook easy to understand!! I don't know what, but I was having trouble understanding this one, but your simple, clear example was just perfect! I looked at like 6 other videos, and yet this one clicked with me. You just got a new subscriber!

  • @onesome7713
    @onesome7713 5 місяців тому

    definitely the best lecture I've ever watched! Thx!

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

    I have seen other videos that cover this topic, but it is in this one that I really understood how it works. Thank you!!

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

    One of best explanation skills I have come across

  • @PriyankaSingh-vm4kv
    @PriyankaSingh-vm4kv 3 роки тому

    No words to describe how easily you explain this concept 👏👏👏

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

    What an Explanation brother! Keep it up. You have amazing talent. Appreciate it.

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

    This is the best video I have seen on useCallback. Thank you so much!!

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

    First time I understand useCallback thanks brother

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

    I really appreciate your logic and hard work. I got stuck in my project and needed to come and understand why it was designed like that. Then your explanation enlightens me with easy examples. You should earn millions of subscriptions.

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

    The way you are explaining is awesome thank you so much for sharing this video and your knowledge.

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

    This is how to explain useCallback. Just Perfect.

  • @HarshSingh-hk8fe
    @HarshSingh-hk8fe 2 роки тому

    this is the best explanation of this hooks , now i know how its working

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

    Thanks for the video really explains the concepts well. I hope you are doing this way to convey the concepts. If you separate salary and age as a different component with its own state, there is no need of memo or callback. You should have mention these kind of tips at the end. Most of us watching the tutorials are beginners while explaining the concepts if you include best practices also would be really helpful. Thanks..!!

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

      This was just an example! Think of a complex application with multiple child components.

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

    Great stuff! Its near impossible to find people who can explain complex topics with such clarity

  • @ינוןאלבז-כ1ז
    @ינוןאלבז-כ1ז 4 роки тому

    Take a complex and scary subject and make it really simple
    You deserve to be appreciated

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

    This is the best explanation..I never comment on any youtube video but This video was really awesome.

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

    Great. I became clear in this topic after viewing this video

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

    I recommend reading the article he put in the description.

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

    Easy to understand because of the good explanation! Thank you

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

    Thank you! This was very informative and well explained.

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

    Very good example. I love see logs to check performance issues!!!

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

    Thank you, Vishwas. Really great explanation.👌👌👌👌👌

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

    Another place you might want to implement useCallback is to maintain referential equality to functions passed into the dependency array for useEffect, etc.

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

    Thanks! I really appreciate your work!

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

    you are a real teacher, thank you for this tutorials

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

    I love your content..You explain concepts in such an easy manner!
    Happy to have found your channel! :)

  • @shorty235z
    @shorty235z 3 роки тому +3

    Not going to lie, this one may took me a few times to rewatch to get my head wrapped around.

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

    Learned an important lesson about performance. Thanks a lot. But I think its not exactly optimized even with the use of useCallback() because the button is also being re-rendered even though only callback function has changed.
    I understand that its passed as a prop and when props change, the component re-renders... but when you look at the UI, the button remains the same... it doesn't need to be re-rendered.
    Will look deeper into this. Thanks.

  • @techycode2217
    @techycode2217 7 місяців тому

    Crystal Clear explanation ❤

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

    omg, you explain like god. Really easy to understand you man. thanks so much :)

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

    Really nice explanation of useCallback hook. I knew about other hooks, but this was alien to me, but you explained it really well. Subscribing for the explanation.

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

    Your videos are great. I would like you to pass function to setState and don't use any dependency in useCallback, so that only the age and salary component will be rerendered and the buttons will not be rerendered.

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

    Really quality stuff. I just have 1 update request. at 10.35 you compare incrementAge and incrementSalary function saying they are not the same which I believe everyone knows already. What I think you missed is that JS (not react) creates different instance/reference of both incrementAge and incrementSalary function at every render. Meaning, the same function declaration has 2 different references in 2 different renders. And that's where useCallback comes into play.
    Feel free to correct me if I am wrong.

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

    Thank you very much explaining the concept in a easy way :)

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

    awesome Explanation and thanks for the blog 🔥

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

    Totally got with the callback concept thank you

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

    Passing 'age' and 'salary' to the dependencies array of useCallback is not required, there is no specific reason that should make do that, in fact, it increases the amount of unnecessary re-renders of the component..

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

      Like I mention in the video, it is to explain the concept..

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

      It is required due to closure. Unless you use an updater function.

  • @7huannp
    @7huannp 4 роки тому

    Excellent. Only this video makes everything clear for me. Thanks

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

    Best explanation I’ve seen, thanks!

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

    Really great video!!! I learned a lot in these 15 Minutes!

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

    Such a good way of teaching!

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

    Awesome explanation!! Thank you very much :)

  • @sujithgoud3535
    @sujithgoud3535 Місяць тому

    The best explanation 💯💯

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

    I have a question as you mention in the last that why we should not always use useCallback so there is is mentioned that it have to work twice like in this example:
    const dispense = candy => {
    setCandies(allCandies => allCandies.filter(c => c !== candy))
    }
    + const dispenseCallback = React.useCallback(dispense, [])
    So why we do something like this yeah it looks clean but we can also do something like this
    const dispenseCallback = React.useCallback(candy => {
    setCandies(allCandies => allCandies.filter(c => c !== candy))
    }, [candy]);
    so is this the same as above?? for cost expense or not

  • @fazliddinfayziev-qg1vg
    @fazliddinfayziev-qg1vg Рік тому

    Bro you explained so great, It is amazing. Thank you.
    But I have one question. It seems me like, useMemo and useCallback hooks are kind a similar to each other. Can You explain difference please. Thank you bro

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

    To make just one rerender and not two : const increnementAge = useCallback(()=>setCount( c=> c+1), [setCount])

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

      I think empty arraay [ ] is also good enough.

  • @asananddevsingh
    @asananddevsingh 3 роки тому +6

    This way will prevent the re-rendering of buttons too.
    const incrementAge = useCallback(() => {
    setAge((prevAge) => prevAge + 1);
    }, []);

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

      How is this happening

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

      @@harshalsingh2443 This is because, I've removed the dependency of useCallback i.e age and using function while updating the state of age, it will automatically give me previous state of age. Remember here handler function is not dependent on age.

  • @AMANKUMAR-jc2fz
    @AMANKUMAR-jc2fz Рік тому

    Great Man thanks 👍 really today I understand the way to use these 😁

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

    thank u for a great and well-explained video!

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

    we can avoid button re-renders too! remove age as dependency!

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

    Codevolution Your just the best

  • @mytech-youtube8436
    @mytech-youtube8436 Рік тому

    Amazing explanation!

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

    useCallback:
    - in short: whenever a component re-renders (which will occur if its state/prop changes or unmount/mount), the states/functions inside it will also be created newly (new instances). So if these states/functions (which got new instances - aka this is meaning changed) - are passed as a prop to any child components, inside this parent component, it will trigger the child component to be re-rendered as well. Thats why useCallback & React.memo are used.
    - Performance Optimization
    -> useCallback( arrowFunction, arrayOfDependentprop )
    chatgpt: Why using a useCallback all the time is not a good idea?

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

    Very well explained , thank you.

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

    excellent explanations. thank you so much for your effort

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

    Amazing explanation.

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

    Greatly explained

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

    Thank you so much, I really really appreciate it :)

  • @Jason-uv5tm
    @Jason-uv5tm 3 роки тому +5

    By the way, can we stop with the useless comments saying how good the series is? Sorry to sound mean, but when I'm trying to get help, these comments really aren't helping. They are good, but please just stop and think if your comment would really help other people.

    • @Ahmed-fq3kz
      @Ahmed-fq3kz Рік тому +1

      I really have the same thought, but i'm answering myself that it is just some tax i will afford for a free content because he get benefits from such comments

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

    The best content ever

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

    Good day sir @Codevolution, the Kentcdodds Blog post link in the description is not working. I'll go and search for it on google for now. Thanks anyways your videos give me great confidence.

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

    Very good understanding.

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

    the performance optimization:
    the problem: in App there are several components, when one componenr is re-rendered, others will be re-rendered as well -> nicht optimal
    useMemo
    - refenrence equality for a function
    - we should not always use the callback function

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

    explained clearly new subscriber here thank you very much

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

    What amazing video! This is great man!

  • @it-series-music
    @it-series-music 3 роки тому

    Should we always use memo and callbackHook in case of state uplifting ?

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

    I learned so much, thanks !

  • @xtynd
    @xtynd 6 місяців тому

    Is it always suggested to use "export default React.memo(Component)" instead of "export default Component"?

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

    awesome as always

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

    Awesome explaination

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

    Great Explanation

  • @benny-shen
    @benny-shen Місяць тому

    great explanation

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

    Wow This is Awesome!! great explaination...

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

    @Codevolution, could you please tell me what color theme are you using for VS Code?

  • @varchas-v8777
    @varchas-v8777 4 роки тому +1

    If we do (prevAge => prevAge + 1) then we can pass empty array as dependency array. This will render only the count component, not the button. Isn't it the most optimized solution? Please explain me if I'm wrong..

  • @Jason-uv5tm
    @Jason-uv5tm 3 роки тому +1

    The dependencies list, according to codesandbox is not needed. If for example, I make the dependency list an empty array ([]), then only the salary is rendering, and everything is fine. Using age or salary inside the dependency list is bad because the button will still be redefined every single time. Why is this?

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

    crystal clear explanation, thank you, muito bom

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

    at 7:00 : When we're only updating the age, then why the "Increment Age Button" needs to re - render ?? Great videos btw.

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

      isnt cause both age and button got updated so they re-render ? click event is update