Learn React Hooks: useCallback - Simply Explained!

  Переглядів 63,973

Cosden Solutions

Cosden Solutions

11 місяців тому

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!

КОМЕНТАРІ: 178
@cosdensolutions
@cosdensolutions Місяць тому
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_ 11 місяців тому
Best react educator found on youtube! Agreed everyone?
@arunkaiser
@arunkaiser 5 місяців тому
I agree
@anabiatahir3772
@anabiatahir3772 5 місяців тому
Agreed
@alidbouk4335
@alidbouk4335 4 місяці тому
Absolutely !
@amalc7814
@amalc7814 4 місяці тому
explains every thing in detail, crisp and clear
@prajwalpandeshwar9967
@prajwalpandeshwar9967 4 місяці тому
Absolutely
@CalltheWei1020
@CalltheWei1020 6 місяців тому
Love how you explain the whys and what really happens behind the scenes. Thanks and much appreciated
@lexsemenenko7044
@lexsemenenko7044 9 місяців тому
Clean and clear explanation with examples on what and why for us to clarify the ideas behind the hook. Again, nailed! Appreciated.
@whiterabbitdev
@whiterabbitdev 5 місяців тому
cheers dude, none of that "learn this in 3 minutes bs" you explain everything nice and clearly!
@gmjitendra
@gmjitendra 7 місяців тому
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
@lagekutsa
@lagekutsa 2 місяці тому
you explain useCallback so simple man, really appreciate.
@tingwen1713
@tingwen1713 2 місяці тому
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!
@SWE507
@SWE507 Місяць тому
This is the cleanest and clearest explanation of useCallback ever!!!
@smokinjoe3000
@smokinjoe3000 3 місяці тому
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
@shamimhossain551
@shamimhossain551 5 місяців тому
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 ❤
@rajeshmaydev
@rajeshmaydev Місяць тому
the words you picked "freeze in time". Really helped me wrap the idea of it after many months of doubt
@noahmilliken4259
@noahmilliken4259 2 місяці тому
Props to Cosden for not bombarding us with some 15 second intro branding. Down to business, love it.
@JakeLuden
@JakeLuden 10 місяців тому
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 9 місяців тому
damn, thanks for saying that! Really means a lot 🤙
@atomvandermerwe3613
@atomvandermerwe3613 9 місяців тому
I 100% agree with @Jakezaruba, this video explained it perfectly.
@ProfessionalClickers
@ProfessionalClickers 8 місяців тому
Finally I understood useCallback ❤️ Thanks a lot!
@vivekkumar-pc1xy
@vivekkumar-pc1xy 5 місяців тому
Great video, well explained. Everything is now crystal clear
@user-be3ri8gf1p
@user-be3ri8gf1p 20 днів тому
Best hooks tutorial I've encountered so far! Thanks 🔥
@catalincatalin4101
@catalincatalin4101 6 місяців тому
We really needed someone make this hooks simple. Thanks
@__shubhamtiwari
@__shubhamtiwari 3 місяці тому
Bhai sahab matalab ekdam gajab padhaya yaar, dil jeet liya dost ❤
@oscardasilva971
@oscardasilva971 11 місяців тому
The last example was very clever. Thank you sir.
@shubham.verma-dev
@shubham.verma-dev 9 місяців тому
Nicely explained. I tried to understand from other videos as well, but this is really good. Now I am done with my doubts.
@cosdensolutions
@cosdensolutions 9 місяців тому
Happy to hear! 🤙
@coolme7437
@coolme7437 3 місяці тому
Thanks, this really is my last useCallback tutorial.
@fhkodama
@fhkodama 3 місяці тому
Oh, finally a good explanation about this hook! You got a subscriber with this video! Thanks!
@uquantum
@uquantum 7 місяців тому
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!
@cosdensolutions
@cosdensolutions 7 місяців тому
makes me happy to hear!
@vesa95
@vesa95 10 місяців тому
Awesome explanation, man! very, very clever!
@legend_749
@legend_749 8 місяців тому
awesome explaination, pls continue this series
@cosdensolutions
@cosdensolutions 8 місяців тому
Will do!
@kisuhara7844
@kisuhara7844 3 місяці тому
literally the best react tutor thank you for your videos
@viliuxsas
@viliuxsas 6 місяців тому
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.
@cosdensolutions
@cosdensolutions 6 місяців тому
glad to hear it!!!
@Gangbuster74
@Gangbuster74 10 місяців тому
By far this is rhe best explanation have watched many many tutorials, but you explained it in very simple terms 🎉ty
@cosdensolutions
@cosdensolutions 10 місяців тому
Glad to hear it! 🤙
@belkocik
@belkocik 11 місяців тому
Very clear explanation!
@MudassirKhan-sx9jy
@MudassirKhan-sx9jy 8 місяців тому
so clear. thank you ♥
@ahmetzeren6878
@ahmetzeren6878 5 місяців тому
great explanation thank you. no blank spaces left
@aleksandrgolyk3787
@aleksandrgolyk3787 2 місяці тому
Great explanation 🎉🎉🎉 i was so struggling with understanding this topic. Thank you🎉🎉🎉
@HenryBabbage
@HenryBabbage 9 місяців тому
Awesome explanation. Thanks!
@Jam....
@Jam.... 5 місяців тому
Excellent clear explanation thank you.
@peirama2
@peirama2 4 місяці тому
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 4 місяці тому
Thank you for the kind words!
@peirama2
@peirama2 4 місяці тому
@@cosdensolutions thank you for the many “aha” moments
@WaseemAhmad-mf3wh
@WaseemAhmad-mf3wh Місяць тому
Thank you for your clear explanation ❤
@briandacallos4234
@briandacallos4234 8 місяців тому
I'm shocked how he explained it very well than anyone could. Automatic subs!
@cosdensolutions
@cosdensolutions 8 місяців тому
Much appreciated! ☺️
@SPribyt
@SPribyt 8 місяців тому
Good job explaining!
@VijayVibhuteFlautist
@VijayVibhuteFlautist 3 місяці тому
Love it! Appreciate the way you explained everything. ❤
@RaGa_BABA
@RaGa_BABA 21 день тому
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
@chengxiaoxia8046
@chengxiaoxia8046 Місяць тому
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 Місяць тому
I didn't know about the memo either ! Thanks very much.
@antimrevapati8992
@antimrevapati8992 2 місяці тому
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!
@cosdensolutions
@cosdensolutions 2 місяці тому
Thank you for the kind words ☺️
@cat_demon4405
@cat_demon4405 8 місяців тому
Underrated channel! subscribed
@attouyassine1056
@attouyassine1056 2 місяці тому
Appreciate the way you explained it, thanks
@zul.overflow
@zul.overflow 2 місяці тому
clean and clear 👍
@rgchroniclestv
@rgchroniclestv 8 місяців тому
Really a big help. Thanks
@borameupai1792
@borameupai1792 Місяць тому
Your explain saved my life. U just got a new subscriber. Thanks.
@2029leandro
@2029leandro 2 місяці тому
Perfectly explained!
@abhinavdhama3014
@abhinavdhama3014 7 місяців тому
Amazing explanation on hooks ❤❤❤❤
@cosdensolutions
@cosdensolutions 7 місяців тому
glad you liked it!
@ASOCMARCTKD
@ASOCMARCTKD 10 днів тому
Excellent explanation, thank you 😇
@Za-xh9tj
@Za-xh9tj 2 місяці тому
Expected to learn useCallback, instead I learned how react rerender, memo, and usecallback works. Amazing.😄
@dilshadazam880
@dilshadazam880 6 місяців тому
You deserve atleast 1 million subscribers. Please keep making such videos
@PradeepManek
@PradeepManek 5 днів тому
Thank you brother you explain this topic very well.
@pythonbrothersandfamily
@pythonbrothersandfamily 27 днів тому
omggg Great explanation. Thank you bro
@sidkan9251
@sidkan9251 7 місяців тому
you got a new subscriber thanks man
@umairiqbal1483
@umairiqbal1483 7 місяців тому
Thanks. It really helped.
@aliksayfullin3278
@aliksayfullin3278 2 місяці тому
Great explanation 👍
@OXIDE777-is6gs
@OXIDE777-is6gs 11 місяців тому
Awesome man! thanks a bunch!
@sukritsaha5632
@sukritsaha5632 11 місяців тому
Great explanation
@alangraton2000
@alangraton2000 6 місяців тому
Dude, your channel is the bomb. Thanks for the vids ^-^
@TheMakeupmonika
@TheMakeupmonika 9 місяців тому
love you man ! thank you so much
@samahgad241
@samahgad241 4 місяці тому
thanks alot, very happy to know your channel🌹
@shabeeeb6400
@shabeeeb6400 6 місяців тому
you earned a sub :) great explanation
@dylcodes
@dylcodes 15 днів тому
Great video, thank you!
@maryannegichohi7063
@maryannegichohi7063 3 місяці тому
Amazing tutorial
@tigranharutyunyan7674
@tigranharutyunyan7674 4 місяці тому
Enjoyed very much
@diggerdog001
@diggerdog001 4 місяці тому
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 😀
@mralextacy
@mralextacy 6 місяців тому
awesome tutorial
@atakan8570
@atakan8570 6 місяців тому
best teacher ever
@madehayoussof
@madehayoussof Місяць тому
Amazing! thanks
@remix_me
@remix_me 11 місяців тому
you earned a new subscriber
@haiderjaafer8164
@haiderjaafer8164 11 місяців тому
Great to see you here I followed you on tiktok. So great work keep going forward
@cosdensolutions
@cosdensolutions 11 місяців тому
Nice! Thanks! Will do ☺️
@mobinhamidi4161
@mobinhamidi4161 Місяць тому
Thanks ❤
@raymondmichael4987
@raymondmichael4987 10 місяців тому
Thanks buddy 😊😊
@nyomansunima
@nyomansunima 7 місяців тому
Good explaination. Now i know it
@cosdensolutions
@cosdensolutions 7 місяців тому
glad to hear it!
@mostinho7
@mostinho7 3 місяці тому
Done thanks clear explanation
@INetreba
@INetreba Місяць тому
brilliant
@bismarckkaine2700
@bismarckkaine2700 7 місяців тому
You're awesome
@VishalSharma-rn7mt
@VishalSharma-rn7mt 4 місяці тому
Awesome
@giannizamora7247
@giannizamora7247 2 місяці тому
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.
@nayandey5010
@nayandey5010 6 місяців тому
Great explanations sir the only concern is I watched 5 ads each containing 2 ads(which I can't skip) 😂😂😂
@iamparmjeetmishra
@iamparmjeetmishra 2 дні тому
thanks
@anonim1468
@anonim1468 3 місяці тому
If we add another state and update it still will get memoized component to rerender. This is because arrays are reference types in JS and in every component render it gets a new reference so you will get new reference for callback function as well
@smchap9474
@smchap9474 6 місяців тому
To understand, with this particular example we did actually undo all the purpose of useCallvack since users will be different when shuffled, right?
@ahallock
@ahallock 3 місяці тому
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.
@matthewwebclar
@matthewwebclar 2 місяці тому
EPic
@abhinavdhama3014
@abhinavdhama3014 7 місяців тому
Can we have some videos on javascript also?
@capt_vj
@capt_vj 2 місяці тому
nice
@satyamkumar6469
@satyamkumar6469 3 місяці тому
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 3 місяці тому
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
@Akhillbj
@Akhillbj 10 місяців тому
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 10 місяців тому
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
@AntNZ
@AntNZ 11 місяців тому
Please do a useReducer tutorial 👏👏👏👏🙌🙌🙌🙌🙏🙏🙏🙏
@cosdensolutions
@cosdensolutions 11 місяців тому
Oh absolutely, very soon!
@nitinmali7430
@nitinmali7430 3 місяці тому
Thanks Cosden. One question. What's the difference example onChange={handleSearch} and onChange={()=>handleSearch()} ?
@cosdensolutions
@cosdensolutions 3 місяці тому
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
@joshikroshan5584
@joshikroshan5584 Місяць тому
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 Місяць тому
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 Місяць тому
@@cosdensolutions thank you
@reactdev2838
@reactdev2838 5 місяців тому
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 5 місяців тому
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 5 місяців тому
@@cosdensolutions Understand, But I really like your video, one of the best tutorials I have ever seen...
@harag9
@harag9 6 місяців тому
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 5 місяців тому
This is an excellent point, I am also interested in finding a solution that solves this problem.
@bq_wang
@bq_wang 8 місяців тому
👍
@unhandledexception1948
@unhandledexception1948 6 місяців тому
@3 min, you discuss wrapping the component with memo() to have react check if input props changed before a re-render. I had initially thought that react did already did that, and so am confused as to why this optimization is necesary What would cause react to re-render componentseven if the props they receive haven't changed.
@unhandledexception1948
@unhandledexception1948 6 місяців тому
Oops.... sorry asked too soon, you explain the problem at 5:00 min when discussing functions in react 🙂
@harag9
@harag9 6 місяців тому
@@unhandledexception1948 Thanks for the question. I've never seen "memo" on a component before now so wasn't sure why, as I too thought react didn't re-render... So my followup question to be sure... should I only use memo on components as in the memo(Search) when I pass functions to the component ? Would passing in a state "setVariable(newval)" function count like the "onHandleX" functions. I've just created a Dummy component with a text property and passed hardcoded text to it then in the component console logged out "rendered". NOT used memo(dummy) -- Even through the property hasn't changed the component renders everytime the page renders. So I don't think react checks for none changing properties. So my next question even for a simple component should we use memo(Dummy) all the time to stop it from rerendering things like titles etc.
@cordial
@cordial 6 місяців тому
After you add 'users' as a dependency to the useCallback, if you press shuffle, would the search component still not rerender?
@cosdensolutions
@cosdensolutions 6 місяців тому
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 6 місяців тому
@@cosdensolutions cheers. great video as always btw.
@samiullahsheikh5015
@samiullahsheikh5015 8 місяців тому
so one should always wrap event handlers with useCallback? if not then why not?
@cosdensolutions
@cosdensolutions 8 місяців тому
only wrap when it's an expensive computation. Otherwise it's not needed!
Learn React Hooks: useContext - Simply Explained!
15:46
Cosden Solutions
Переглядів 109 тис.
Learn React Hooks: useMemo - Simply Explained!
13:41
Cosden Solutions
Переглядів 68 тис.
Спектакль для окупантів та ждунів 🤯
00:47
Радіо Байрактар
Переглядів 542 тис.
You Are Using useEffect Wrong
14:40
Cosden Solutions
Переглядів 28 тис.
Learn React Hooks: useReducer - Simply Explained!
13:48
Cosden Solutions
Переглядів 38 тис.
Зачем на самом деле нужен хук useCallback
8:33
Михаил Непомнящий
Переглядів 36 тис.
You might not need useEffect() ...
21:45
Academind
Переглядів 123 тис.
Learn React Hooks: useEffect - Simply Explained!
14:07
Cosden Solutions
Переглядів 98 тис.
Learn React Hooks: useRef - Simply Explained!
12:42
Cosden Solutions
Переглядів 69 тис.
Efficiently Render 100,000 Rows in React
16:41
Cosden Solutions
Переглядів 13 тис.
Спектакль для окупантів та ждунів 🤯
00:47
Радіо Байрактар
Переглядів 542 тис.