Learn useCallback In 8 Minutes

Sdílet
Vložit
  • čas přidán 3. 07. 2024
  • 🚨 IMPORTANT:
    Full React Course: courses.webdevsimplified.com/...
    In this video I cover everything you need to know about the useCallback hook. I go over all the main use cases for useCallback as well as many common mistakes that developers make. This is part of a series of React videos where I cover all the important hooks in React.
    📚 Materials/References:
    useCallback Blog Article: blog.webdevsimplified.com/202...
    React Hooks Playlist: • React Hooks
    🧠 Concepts Covered:
    - How to use hooks in React
    - How to manage referential equality of functions in React
    - How to use the useCallback hook
    🌎 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
    #ReactJs #WDS #useCallback

Komentáře • 259

  • @nanonkay5669
    @nanonkay5669 Před 4 lety +776

    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 Před 3 lety +4

      Thanks man!

    • @h2o313
      @h2o313 Před 3 lety +21

      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 Před 3 lety +6

      Why can't we use useEffect instead of
      useCallback?

    • @vijaykumarreddyalavala3713
      @vijaykumarreddyalavala3713 Před 3 lety +44

      @@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 Před 3 lety +2

      @@vijaykumarreddyalavala3713 Freaking nicely explained

  • @pawandeore6989
    @pawandeore6989 Před rokem +59

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

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

      @@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.

  • @tomaszkapalka
    @tomaszkapalka Před 2 lety +29

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

  • @davidserranoii4114
    @davidserranoii4114 Před 4 lety +8

    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

  • @hamzafaysal909
    @hamzafaysal909 Před 4 lety +5

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

  • @Shoulon
    @Shoulon Před 3 lety +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!

  • @stevereid636
    @stevereid636 Před 3 lety +4

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

  • @oracleking4252
    @oracleking4252 Před měsícem +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

  • @markwillis1045
    @markwillis1045 Před 3 lety +5

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

  • @angell2341
    @angell2341 Před 3 lety +3

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

  • @zdargahi
    @zdargahi Před 2 lety +2

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

  • @mohmarroun3189
    @mohmarroun3189 Před rokem +2

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

  • @borisnekezov6620
    @borisnekezov6620 Před 2 lety

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

  • @simeonieroteev4555
    @simeonieroteev4555 Před 3 lety

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

  • @sottitron
    @sottitron Před 3 lety +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.

  • @andrewshaban2888
    @andrewshaban2888 Před rokem

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

  • @johnhaupenthal
    @johnhaupenthal Před 3 lety +1

    Clear and concise, great video.

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

    Best tuts on the web - PERIOD. Thanks Kyle!

  • @tadeuszjiwu255
    @tadeuszjiwu255 Před 3 lety

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

  • @mrnabby4178
    @mrnabby4178 Před rokem

    The topic is crystal clear. Thank you man.

  • @KaaiSpeaksHisMind
    @KaaiSpeaksHisMind Před 3 lety

    Beautifully explained, thank you.

  • @markwillis2336
    @markwillis2336 Před 2 lety +1

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

  • @marcelotorreblanca9276
    @marcelotorreblanca9276 Před 2 lety +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!!!

  • @logio7663
    @logio7663 Před 3 lety

    Very clear explanation. Thanks a lot ! !

  • @davidgentilli7944
    @davidgentilli7944 Před 3 lety

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

    • @frontendHacks
      @frontendHacks Před 2 lety

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

  • @developerbuddyavikunj5996

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

  • @aryanshmahato
    @aryanshmahato Před 4 lety

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

  • @thebetoxpro
    @thebetoxpro Před 2 lety

    Amazing explanation. Thank you from Mexico

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt Před 3 lety +1

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

  • @maor44
    @maor44 Před 2 lety

    Amazing work man! thank you!

  • @GrigorMinasyan
    @GrigorMinasyan Před 3 lety +3

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

  • @jaytran247
    @jaytran247 Před 3 lety

    Super genius explanation. Thanks so much.

  • @hoyinli7462
    @hoyinli7462 Před 2 lety

    super clear. highly recommend this channel :)

  • @vivarantx
    @vivarantx Před 2 lety

    great man, you deserve great success

  • @ifillup
    @ifillup Před 4 lety

    Great explanation (again)!

  • @NITESHSINGHNRS
    @NITESHSINGHNRS Před 3 lety

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

  • @HOLONIA4EVER
    @HOLONIA4EVER Před 3 lety

    Amazing explanation !

  • @keiferramos8858
    @keiferramos8858 Před 2 lety

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

  • @rileykarl4895
    @rileykarl4895 Před 2 lety

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

  • @johannesperez9548
    @johannesperez9548 Před rokem

    Crystal Clear, thank you so much!

  • @bek3634
    @bek3634 Před rokem

    really simple explanation! thanks to you a lot.

  • @iurii7752
    @iurii7752 Před 4 lety

    Great explanation :) Thank you

  • @Karlponken
    @Karlponken Před rokem

    Great explanation! Thanks a lot!

  • @mayankvora8116
    @mayankvora8116 Před 3 lety

    Awesome Explanation

  • @davidhahn7391
    @davidhahn7391 Před 4 lety

    great explanation! thank you.

  • @lightrao
    @lightrao Před rokem

    Thank you, very useful !

  • @luaneaquino7835
    @luaneaquino7835 Před 3 lety

    Thank you! great tutorial

  • @shivanyachandra4214
    @shivanyachandra4214 Před 3 lety +2

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

  • @derekmoore7401
    @derekmoore7401 Před 3 lety

    very helpful and succinct. Thanks for your videos!

  • @shizasiddiqui1689
    @shizasiddiqui1689 Před 3 lety

    You're brilliant Kyle.

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

    Great explanation!

  • @kwaku_2023
    @kwaku_2023 Před 2 lety

    You are a blessing!

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

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

  • @ibrahimyoussef4489
    @ibrahimyoussef4489 Před rokem

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

  • @eshwarravikanti7659
    @eshwarravikanti7659 Před 3 lety

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

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

    Thank you so much for this video

  • @manishkumartripathi2259

    Nice explanation 😊

  • @nihadyaqublu2359
    @nihadyaqublu2359 Před rokem

    thank you for the video!

  • @shane2023amazon
    @shane2023amazon Před 3 lety

    concise and helpful

  • @lakshyaagarwal4044
    @lakshyaagarwal4044 Před 3 lety

    what a wonderful example !!!

  • @Stef-an
    @Stef-an Před 2 lety

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

    • @COROLOSTO
      @COROLOSTO Před rokem +1

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

    • @saarza9991
      @saarza9991 Před 17 dny

      ​@@COROLOSTO we all are noobie poobies :)

  • @jefersoncosta2621
    @jefersoncosta2621 Před 3 lety

    awesome!! U rock, thank you so much for this video.

  • @naserpapi8921
    @naserpapi8921 Před rokem

    Excellent
    Thanks

  • @sergiyrudenko905
    @sergiyrudenko905 Před 3 lety

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

  • @Wakkyguy
    @Wakkyguy Před 4 lety +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  Před 4 lety +23

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

    • @Wakkyguy
      @Wakkyguy Před 4 lety +2

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

    • @guidoglielmi7992
      @guidoglielmi7992 Před 2 lety +2

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

    • @GreatAdib
      @GreatAdib Před rokem

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

  • @cyberwolf9543
    @cyberwolf9543 Před rokem

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

  • @xylvnking
    @xylvnking Před rokem +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

  • @kose241
    @kose241 Před rokem

    you helped me get a job at coinbase, ty

  • @lemon_maho
    @lemon_maho Před rokem

    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 ^^

  • @AnoJlJloH
    @AnoJlJloH Před 3 lety

    You're the BEST!!!

  • @nishaindesilva3738
    @nishaindesilva3738 Před 2 lety

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

  • @bibahbibah5108
    @bibahbibah5108 Před 2 lety

    really u have simpleified to me

  • @thecutedreamkostasp.4449

    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!

  • @mohammadsaadati2863
    @mohammadsaadati2863 Před 2 lety

    Tnx very useful

  • @ritavdas7570
    @ritavdas7570 Před rokem

    Amazing video

  • @neisservilla7239
    @neisservilla7239 Před 3 lety

    thanks you really help me

  • @blakelarson5005
    @blakelarson5005 Před 3 lety

    Great video

  • @Bruno-ds8ze
    @Bruno-ds8ze Před 4 lety

    finally thankyou very much

  • @prince5922
    @prince5922 Před rokem

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

  • @eldowado
    @eldowado Před rokem

    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!😁

  • @yag7769
    @yag7769 Před 4 lety

    Amazing, thankss

  • @Johnny-rn8fb
    @Johnny-rn8fb Před 2 lety

    Thanks

  • @alisherdotdev
    @alisherdotdev Před 2 lety

    thanks Kyle :))

  • @satindersingh9671
    @satindersingh9671 Před 3 lety

    You are the best

  • @rahuldwivedi4758
    @rahuldwivedi4758 Před 6 měsíci +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)

  • @goharamin820
    @goharamin820 Před 3 lety

    thank you soo much

  • @DediAnanto
    @DediAnanto Před rokem

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

  • @AndrewDevUA
    @AndrewDevUA Před 2 lety

    awesome!

  • @Neha-sw6ky
    @Neha-sw6ky Před 4 lety +1

    Loveeee from India❤❤❤

  • @yahya89able
    @yahya89able Před 2 lety

    fantastic

  • @lukisIVIII
    @lukisIVIII Před 3 lety

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

  • @vinzbrain
    @vinzbrain Před 2 lety +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 Před 2 lety +2

      I like the second question, I wonder the same.

  • @sudhansupradhan6891
    @sudhansupradhan6891 Před 3 lety

    Awesome 👌

  • @arbobmehmood5831
    @arbobmehmood5831 Před 3 lety

    Nailed it

  • @exoticcoder5365
    @exoticcoder5365 Před rokem

    Great !

  • @snarkykat
    @snarkykat Před 4 lety +1

    Congratulations on your engagement :)

  • @Andy-si1pl
    @Andy-si1pl Před rokem

    my go to guy

  • @tinmank
    @tinmank Před 3 lety

    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.

  • @abhilashreddy8724
    @abhilashreddy8724 Před 3 lety

    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?

  • @ridl27
    @ridl27 Před 4 lety

    ty.