Learn useCallback In 8 Minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 265

  • @nanonkay5669
    @nanonkay5669 4 ปีที่แล้ว +814

    useMemo: Returns and stores the calculated value of a function in a variable
    useCallBack: Returns and stores the actual function itself in a variable

    • @GuitarreroDaniel
      @GuitarreroDaniel 4 ปีที่แล้ว +4

      Thanks man!

    • @h2o313
      @h2o313 4 ปีที่แล้ว +23

      good summary. I often found "useCallBack" is handy when you're passing the parent's setState function down to its children.

    • @Vishal-ki2df
      @Vishal-ki2df 3 ปีที่แล้ว +6

      Why can't we use useEffect instead of
      useCallback?

    • @vijaykumarreddyalavala3713
      @vijaykumarreddyalavala3713 3 ปีที่แล้ว +46

      @@Vishal-ki2df Using useEffect you can run a function when a variable changes. But that does not mean that function will not run when the other state variables in your component change. Using useCallback you can make sure that the function runs exactly when you want it to run based on changes in a variable instead of everytime the whole component renders.

    • @ParadoxWorks
      @ParadoxWorks 3 ปีที่แล้ว +3

      @@vijaykumarreddyalavala3713 Freaking nicely explained

  • @pawandeore6989
    @pawandeore6989 ปีที่แล้ว +82

    for someone who is confused the main difference between useMemo and useCallback is usMemo cache values and useCallback caches the function itself

    • @pawandeore6989
      @pawandeore6989 7 หลายเดือนก่อน

      @@xlgnepoTechnically, you can achieve a similar effect by using useMemo to memoize a function, but it's not semantically the same as using useCallback. useCallback is specifically designed to memoize functions to ensure stable identities across renders, which is crucial for performance optimizations, especially when passing functions as props to child components. It's more explicit and conveys the intention more clearly. So while useMemo can be used for this purpose, useCallback is the recommended approach for memoizing functions in React components.

  • @oracleking4252
    @oracleking4252 6 หลายเดือนก่อน +1

    I love this guy. He breaks it down!!!! Wherever I am, whatever mess I am in, I look for him to solve me problems. He's my personal full stack overflow

  • @tomaszkapalka
    @tomaszkapalka 3 ปีที่แล้ว +31

    I am always impressed how you can explain quite complicated things in a simple way! As always...great job and many thanks!

  • @Shoulon
    @Shoulon 4 ปีที่แล้ว +21

    This is really awesome. I was building a grid that was initially built from a function then would set it to state. But my other components would force it to rebuild everytime. UseCallback fixed this issue!

  • @GrigorMinasyan
    @GrigorMinasyan 3 ปีที่แล้ว +3

    So helpful! I had an issue where socket connection would get duplicated when toggling dark more

  • @davidserranoii4114
    @davidserranoii4114 4 ปีที่แล้ว +9

    Your understanding is inspirational. The biggest take away from this video im getting is to look behind the scenes to improve your code so you can run as efficiently as possible. Always look forward to your videos

  • @rahuldwivedi4758
    @rahuldwivedi4758 11 หลายเดือนก่อน +1

    Great work. But here’s the issue. The useEffect wouldn’t be called again in your List component as you have mentioned. But whatever inside your return is, will be re-rendered again. Put a console log inside the return block of list component to see. To prevent that you need to wrap the List component with memo, memo(List)

  • @marcelotorreblanca9276
    @marcelotorreblanca9276 2 ปีที่แล้ว +1

    I've been having a really bad time looking at the React documention about those hooks. And just when I was wondering about the difference between useMemo and useCallback: Boom!! you read my mind!! Just outstanding explanation!!!

  • @hamzafaysal909
    @hamzafaysal909 4 ปีที่แล้ว +5

    Just watched your useMemo video and I was really confused between these 2 hooks but this video clears everything... Thanks Kyle : )

  • @angell2341
    @angell2341 4 ปีที่แล้ว +3

    Explaining the usecase on why we have to use that is really ausum, I felt ur channel unique

  • @vinzbrain
    @vinzbrain 3 ปีที่แล้ว +7

    Great video! 2 questions:
    1) wouldn't it not make more sense to pass the generated array to List rather than the function that generates them?
    2) can useCallback be achieved with useMemo that that wraps a function that returns a function?

    • @rajk1623
      @rajk1623 2 ปีที่แล้ว +2

      I like the second question, I wonder the same.

  • @xylvnking
    @xylvnking 2 ปีที่แล้ว +1

    this one gave me a lot of trouble for some reason despite the other hooks not being too tough. the way i wrapped my brain around it is to remember that useCallback allows us to pass a reference to a function, whereas passing a regular function as a prop causes react to create a new function within the component on each render.
    useMemo allows us to avoid expensive variable calculations, and useCallback allows us to avoid redundant functions being created in memory.
    if I'm wrong PLEASE let me know lol

  • @lemon_maho
    @lemon_maho ปีที่แล้ว

    Mannnn I was actually going to search for a solution to my problem, and then without me even searching, I see this video and I'm like : sure let's check it, just for it be the solution I'm looking for, now I'm off to use that in my code, thank you ^^

  • @stevereid636
    @stevereid636 3 ปีที่แล้ว +5

    This is awesome. I’ve been wondering about the differences for ages. I feel like Kyle just granted me a super power 😎

  • @Wakkyguy
    @Wakkyguy 4 ปีที่แล้ว +43

    It would be very helpful if you could make a useMemo() vs useCallback() video demonstrating their practical usage in one sample project.

    • @WebDevSimplified
      @WebDevSimplified  4 ปีที่แล้ว +23

      I have a blog post linked in the description which does a good job of comparing them to each other.

    • @Wakkyguy
      @Wakkyguy 4 ปีที่แล้ว +2

      @@WebDevSimplified Yes I have read that. Just wanted to see them being used in a single project.

    • @guidoglielmi7992
      @guidoglielmi7992 2 ปีที่แล้ว +2

      lt bugs me that useMemo(()=> ()=> [number, number + 1, number + 2]) would be the same as using useCallback

    • @GreatAdib
      @GreatAdib 2 ปีที่แล้ว

      it can be used in search feature just use debounce an pass it to useCallback.

  • @zdargahi
    @zdargahi 2 ปีที่แล้ว +2

    you explain difficult concepts so clear and simple. Web Dev simplified, indeed

  • @mohmarroun3189
    @mohmarroun3189 ปีที่แล้ว +2

    Thanks , I am always impressed how you can explain quite complicated things in a simple way 👍👍

  • @Stef-an
    @Stef-an 2 ปีที่แล้ว

    These short videos are life-savers for juniors. Thanks a lot!

    • @COROLOSTO
      @COROLOSTO ปีที่แล้ว +1

      And mids :p, oh wait, that means that I'm not mid lvl? oh man! :(

    • @saarza9991
      @saarza9991 5 หลายเดือนก่อน

      ​@@COROLOSTO we all are noobie poobies :)

  • @simmzzzz
    @simmzzzz 11 หลายเดือนก่อน

    Best tuts on the web - PERIOD. Thanks Kyle!

  • @larcho
    @larcho ปีที่แล้ว

    Awesome video. There quite a few videos here in youtube that explains this poorly, thanks for this.

  • @markwillis1045
    @markwillis1045 4 ปีที่แล้ว +5

    Ha, I’m so glad I found this channel. I’m going to be busy watching these the next few days! 😀

  • @andrewshaban2888
    @andrewshaban2888 ปีที่แล้ว

    You are a good tutor. This is a rare talent. Thank you very much!

  • @NITESHSINGHNRS
    @NITESHSINGHNRS 4 ปีที่แล้ว

    bro....your explanation is to diff from others and have more details. keep it up.. superb

  • @thecutedreamkostasp.4449
    @thecutedreamkostasp.4449 2 ปีที่แล้ว

    Excellent show case of usecallback! But i wanted to mention that u could avoid usecallback there if u just out the function outside the App Component and just pass the number as argument on it! Then every time App component rerenders the function wouldn't generated again! Big loves for your channel!

  • @nishaindesilva3738
    @nishaindesilva3738 2 ปีที่แล้ว

    thanks! this solve a major confusion in react. I always wondered why inner functional component resets on parental state update. the answer is useCallback() !

  • @prince5922
    @prince5922 2 ปีที่แล้ว

    6:19 You can pass parameters using useMemo, from react documentation, the following. useCallback(fn, deps) is equivalent to useMemo((yourParameters) => fn, deps).

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt 3 ปีที่แล้ว +1

    awesome tutorial, when I will have money, I will definitely buy your course brother

  • @cyberwolf9543
    @cyberwolf9543 ปีที่แล้ว

    I got this BRO )) THANKS TO YOU!!!
    AFTER 2 YEARS NOTHING CHANGED ABOUT USECALLBACK BRO?

  • @rileykarl4895
    @rileykarl4895 2 ปีที่แล้ว

    You and net ninja is a killer combo - thank for the great vids !

  • @StephanHaewss
    @StephanHaewss 2 ปีที่แล้ว +1

    From the docs "useCallback(fn, deps) is equivalent to useMemo(() => fn, deps)", so it is just a shorter way for memorizing functions as I understand it. Anyway, good video!

    • @naveedalirehmani4135
      @naveedalirehmani4135 2 ปีที่แล้ว

      So the main difference between useMemo and useCallBack is that one returns the value of the function and the other returns the actual function, but what if I use useMemo and return a function as the return value. will it work the same or am I missing something here?
      Like this.
      const functionWithMemo = useMemo(()=>function(){})

    • @StephanHaewss
      @StephanHaewss 2 ปีที่แล้ว +1

      @@naveedalirehmani4135 As I understand it, this is equivalent to useCallback, yes.

  • @mrnabby4178
    @mrnabby4178 ปีที่แล้ว

    The topic is crystal clear. Thank you man.

  • @kose241
    @kose241 2 ปีที่แล้ว

    you helped me get a job at coinbase, ty

  • @markwillis2336
    @markwillis2336 3 ปีที่แล้ว +1

    OooF! This is just pure gold. You just helped me out of a hole again Kyle, thank you so much!

  • @simeonieroteev4555
    @simeonieroteev4555 3 ปีที่แล้ว +1

    That explanation is very, very clear and useful! Thank you!

  • @shivanyachandra4214
    @shivanyachandra4214 3 ปีที่แล้ว +2

    Great video!! can you make a separate based on the difference between useMemo and useCallback, this will really be helpful!

  • @sottitron
    @sottitron 4 ปีที่แล้ว +1

    Enjoying your hooks videos. Your video has a function passed into a component. It makes me think I'd really like to see a course or series on solving some of the trickier design problems with React. Specifically, concepts I've caught myself getting tripped up in my thinking with are: How to decide to use props, state or a store/reducer (and when to use context.) Strategies for interacting with APIs. When passing a function to a component makes sense. Testing your components when you're using hooks.

  • @borisnekezov6620
    @borisnekezov6620 2 ปีที่แล้ว

    Thanks for that video, Kyle! The best and simplest way to understand useCallback!

  • @ralfrolfen5504
    @ralfrolfen5504 ปีที่แล้ว

    Use case: when you pass the a function, created inside a component, as callback into another component ( results in the use case of referential equality, but might not necessarily be clear to everyone)

  • @fabiojonathanarifin1
    @fabiojonathanarifin1 หลายเดือนก่อน

    very clear explanation, thankyou!

  • @csablons
    @csablons 4 ปีที่แล้ว +5

    I'm not sure using useCallback avoid to redraw the component in this case. What you show is to avoid go back in the useEffect. It's different.
    If I put a console.log inside the component List return's, I always get the log.

    • @rahuljawale4461
      @rahuljawale4461 ปีที่แล้ว

      Exactly

    • @fat-man-psycho7647
      @fat-man-psycho7647 2 หลายเดือนก่อน

      This comment needs to be up there, The useCallback() here doesn't prevent a re-render of the child component.

  • @DediAnanto
    @DediAnanto 2 ปีที่แล้ว

    Thanks, for beginner like me, I rarely use useCallback if my editor don't suggest it 😄

  • @ameybhatkar7980
    @ameybhatkar7980 3 ปีที่แล้ว

    I bought your react course but it look simple. If you can make it more comprehensive that will help.

    • @WebDevSimplified
      @WebDevSimplified  3 ปีที่แล้ว

      I appreciate the support. If you feel the course is not a good fit for you and you would like a refund please send me an email. I also plan to update the course when React 18 launches officially.

  • @vivarantx
    @vivarantx 2 ปีที่แล้ว

    great man, you deserve great success

  • @iamprasad88
    @iamprasad88 11 หลายเดือนก่อน

    Use callback is equivalent to using useMemo with a function that returns a function that needs to run when component rerenders. It just avoids the need to curry the function yourself.

  • @thebetoxpro
    @thebetoxpro 3 ปีที่แล้ว

    Amazing explanation. Thank you from Mexico

  • @johnhaupenthal
    @johnhaupenthal 4 ปีที่แล้ว +1

    Clear and concise, great video.

  • @developerbuddyavikunj5996
    @developerbuddyavikunj5996 2 ปีที่แล้ว

    It was really helpful 🤗🙏 . Thanks alot for sharing. Finally got clarity on the concept.

  • @bek3634
    @bek3634 ปีที่แล้ว

    really simple explanation! thanks to you a lot.

  • @logio7663
    @logio7663 3 ปีที่แล้ว

    Very clear explanation. Thanks a lot ! !

  • @aryanshmahato
    @aryanshmahato 4 ปีที่แล้ว

    Desktop in introduction was looking dope! Great video BTW..

  • @tadeuszjiwu255
    @tadeuszjiwu255 3 ปีที่แล้ว

    This is a very clear explanation, thank you very much, keep up the good work

  • @jaytran247
    @jaytran247 4 ปีที่แล้ว

    Super genius explanation. Thanks so much.

  • @hoyinli7462
    @hoyinli7462 2 ปีที่แล้ว

    super clear. highly recommend this channel :)

  • @DanielShamany
    @DanielShamany ปีที่แล้ว

    In my humble opinion, making the comparison between useMemo and useCallback confuses the listener with regards to the concept. By drawing connections to the developer, you won't be teaching the audience to think of concepts so it makes sense to the audience.
    I love your content thought!

  • @keiferramos8858
    @keiferramos8858 2 ปีที่แล้ว

    very true. You really simplified it.Good Job😌.

  • @manishkumartripathi2259
    @manishkumartripathi2259 ปีที่แล้ว

    Nice explanation 😊

  • @davidgentilli7944
    @davidgentilli7944 3 ปีที่แล้ว

    Thank you, Kyle. This explanation is incredibly clear and easy to understand.

    • @frontendHacks
      @frontendHacks 2 ปีที่แล้ว

      watch useCallback Hook in Hindi in easy way explained
      th-cam.com/video/Gj68rN0vLSc/w-d-xo.html&ab_channel=Front-EndHacks

  • @ibrahim_youssef_13.12
    @ibrahim_youssef_13.12 2 ปีที่แล้ว

    Wow!!! awesome I swear this lesson the real best lesson off all I get in this point and this always you

  • @HOLONIA4EVER
    @HOLONIA4EVER 4 ปีที่แล้ว

    Amazing explanation !

  • @shizasiddiqui1689
    @shizasiddiqui1689 3 ปีที่แล้ว

    You're brilliant Kyle.

  • @ahmadbenchakhtir5787
    @ahmadbenchakhtir5787 6 หลายเดือนก่อน

    7:00 I believe we may also need UseCallback if we pass it to a component that holds a lot of nested children inside it, even if the creation process is simple.

  • @mayankvora8116
    @mayankvora8116 4 ปีที่แล้ว

    Awesome Explanation

  • @eldowado
    @eldowado 2 ปีที่แล้ว

    Hey man - love these videos, you seem to have one on every concept. I **think** I can throw you some suggestions to make your audio even crisper, without changing your hardware at all. If you're interested, we can give it a go - then I can maybe ask you some React questions in return, haha.
    Be well, mate!😁

  • @snarkykat
    @snarkykat 4 ปีที่แล้ว +1

    Congratulations on your engagement :)

    • @Cloud-577
      @Cloud-577 4 ปีที่แล้ว

      how do you know?

    • @snarkykat
      @snarkykat 4 ปีที่แล้ว

      @@Cloud-577 It was on Twitter

  • @levk4195
    @levk4195 4 ปีที่แล้ว

    If you really want to understand it, 8 minutes is not enough. Before he released the video, he learned it for a few days. Thats just how it is

  • @Karlponken
    @Karlponken 2 ปีที่แล้ว

    Great explanation! Thanks a lot!

  • @johannesperez9548
    @johannesperez9548 ปีที่แล้ว

    Crystal Clear, thank you so much!

  • @plopplippityplopyo
    @plopplippityplopyo 2 ปีที่แล้ว

    Another thing you would want to use usecall back for is if you are going to debounce something

  • @tinmank
    @tinmank 4 ปีที่แล้ว

    Hello, are you planning to make a simple login sample for React? What are the best practices? How about firebase?
    Most samples are using old class methods, but I really would like to make one with Hooks. should I get data with useMemo?
    or another thing I can't decide; how to use a different menu for logged in and logged out users.
    Thank you for your clean instructions. Best.

  • @maor44
    @maor44 2 ปีที่แล้ว

    Amazing work man! thank you!

  • @cepmadbrozzer4448
    @cepmadbrozzer4448 ปีที่แล้ว

    Are you sure useCallback could help with skipping creating huge functions? I mean, yes, memoization definitely happens, but as far as I get it, it doesn't skip new function creation, it just returns pointer to older one, leavin new one to GC. I can't imagine which browser mechanics allows developer to skip function compiling in a runtime when interpreter reaches function declaration.

  • @jepqmw
    @jepqmw 2 ปีที่แล้ว

    Good explanation of useCallback doesn't ex... oh

  • @verygood5377
    @verygood5377 ปีที่แล้ว

    5:32 Callback allows parameter passing

  • @sreekumarmenon
    @sreekumarmenon 4 ปีที่แล้ว +10

    thanks, useCallback "simplified"!

  • @lakshyaagarwal4044
    @lakshyaagarwal4044 3 ปีที่แล้ว

    what a wonderful example !!!

  • @shane2023amazon
    @shane2023amazon 3 ปีที่แล้ว

    concise and helpful

  • @Neha-sw6ky
    @Neha-sw6ky 4 ปีที่แล้ว +1

    Loveeee from India❤❤❤

  • @luaneaquino7835
    @luaneaquino7835 3 ปีที่แล้ว

    Thank you! great tutorial

  • @thrash_jesus
    @thrash_jesus 3 หลายเดือนก่อน

    You're a G man love the vids

  • @lightrao
    @lightrao 2 ปีที่แล้ว

    Thank you, very useful !

  • @mohammadsaadati2863
    @mohammadsaadati2863 2 ปีที่แล้ว

    Tnx very useful

  • @eshwarravikanti7659
    @eshwarravikanti7659 3 ปีที่แล้ว

    Great explanation!!!!! More power to you:)

  • @jnmldo
    @jnmldo 11 หลายเดือนก่อน

    Thank you so much for this video

  • @iurii7752
    @iurii7752 4 ปีที่แล้ว

    Great explanation :) Thank you

  • @abhilashreddy8724
    @abhilashreddy8724 3 ปีที่แล้ว

    Thanks for the great video! If a parent component's state is updated will it rerender the child component or not? So, in this case, if the number is changed will it rerender the List component irrespective of useCallback?

  • @derekmoore7401
    @derekmoore7401 3 ปีที่แล้ว

    very helpful and succinct. Thanks for your videos!

  • @lukisIVIII
    @lukisIVIII 4 ปีที่แล้ว

    You're the best teacher! You have a true gift, thank you!

  • @ifillup
    @ifillup 4 ปีที่แล้ว

    Great explanation (again)!

  • @naserpapi8921
    @naserpapi8921 ปีที่แล้ว

    Excellent
    Thanks

  • @bibahbibah5108
    @bibahbibah5108 2 ปีที่แล้ว

    really u have simpleified to me

  • @DillionMegida
    @DillionMegida 2 ปีที่แล้ว

    Thanks so much for this video. But there's something I find confusing. The List component is also re-rendered when the state changes, which means it is remounted and a new `useEffect` hook redeclared. So how exactly does this new mounted List component keep track of the previous useEffect dependency state?

    • @BorisDePooter
      @BorisDePooter 2 ปีที่แล้ว

      If you want components to only rerender when certain props change you can wrap the export with React.memo(). This makes sure it only rerenders when the props actually change in contrast to the basic functionality that always rerenders all child components when the parent component rerenders. Note that u still need to use a useCalback function when passing a function as a prop since the reference to this function will still change if you are not using a useCallback

  • @kwaku_2023
    @kwaku_2023 2 ปีที่แล้ว

    You are a blessing!

  • @davidhahn7391
    @davidhahn7391 4 ปีที่แล้ว

    great explanation! thank you.

  • @nihadyaqublu2359
    @nihadyaqublu2359 2 ปีที่แล้ว

    thank you for the video!

  • @sergiyrudenko905
    @sergiyrudenko905 3 ปีที่แล้ว

    you da best, bro!!! thanks!!!

  • @thefanbase
    @thefanbase 4 ปีที่แล้ว +19

    Why would you make the useEffect depend on that *function* in the first place? You can just make it depend on the state `number` right? and this problem would be avoided, it just doesn't make sense.

    • @ea9849
      @ea9849 2 ปีที่แล้ว +2

      It's an example bro

    • @hierotsu
      @hierotsu 2 ปีที่แล้ว +11

      @@ea9849examples should have meaning for this kind of videos, mainly for React.

    • @kirankumar-ki3ci
      @kirankumar-ki3ci ปีที่แล้ว

      Exactly my question

    • @bishwajeetpandey1570
      @bishwajeetpandey1570 ปีที่แล้ว +1

      Relax bro it is just an example to show how useCallback work

  • @ironsand
    @ironsand 10 หลายเดือนก่อน

    Is there any use of useCallback other than passing the memoized function in a dependency array? This is the only example I ever see.

  • @robx_001
    @robx_001 2 ปีที่แล้ว

    Thanks

  • @DeepakGupta-hj2dv
    @DeepakGupta-hj2dv 4 ปีที่แล้ว

    Hey Kyle..please cover redux concept