Learn useCallback In 8 Minutes

  Рет қаралды 481,293

Web Dev Simplified

Web Dev Simplified

Күн бұрын

Пікірлер: 265
@nanonkay5669
@nanonkay5669 4 жыл бұрын
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 жыл бұрын
Thanks man!
@h2o313
@h2o313 4 жыл бұрын
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 4 жыл бұрын
Why can't we use useEffect instead of useCallback?
@vijaykumarreddyalavala3713
@vijaykumarreddyalavala3713 3 жыл бұрын
@@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 жыл бұрын
@@vijaykumarreddyalavala3713 Freaking nicely explained
@pawandeore6989
@pawandeore6989 Жыл бұрын
for someone who is confused the main difference between useMemo and useCallback is usMemo cache values and useCallback caches the function itself
@pawandeore6989
@pawandeore6989 8 ай бұрын
@@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 3 жыл бұрын
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 жыл бұрын
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!
@oracleking4252
@oracleking4252 7 ай бұрын
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
@davidserranoii4114
@davidserranoii4114 4 жыл бұрын
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 4 жыл бұрын
Just watched your useMemo video and I was really confused between these 2 hooks but this video clears everything... Thanks Kyle : )
@angell2341
@angell2341 4 жыл бұрын
Explaining the usecase on why we have to use that is really ausum, I felt ur channel unique
@stevereid636
@stevereid636 3 жыл бұрын
This is awesome. I’ve been wondering about the differences for ages. I feel like Kyle just granted me a super power 😎
@marcelotorreblanca9276
@marcelotorreblanca9276 2 жыл бұрын
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!!!
@GrigorMinasyan
@GrigorMinasyan 3 жыл бұрын
So helpful! I had an issue where socket connection would get duplicated when toggling dark more
@zdargahi
@zdargahi 3 жыл бұрын
you explain difficult concepts so clear and simple. Web Dev simplified, indeed
@markwillis1045
@markwillis1045 4 жыл бұрын
Ha, I’m so glad I found this channel. I’m going to be busy watching these the next few days! 😀
@mohmarroun3189
@mohmarroun3189 Жыл бұрын
Thanks , I am always impressed how you can explain quite complicated things in a simple way 👍👍
@simmzzzz
@simmzzzz Жыл бұрын
Best tuts on the web - PERIOD. Thanks Kyle!
@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).
@Stef-an
@Stef-an 3 жыл бұрын
These short videos are life-savers for juniors. Thanks a lot!
@COROLOSTO
@COROLOSTO Жыл бұрын
And mids :p, oh wait, that means that I'm not mid lvl? oh man! :(
@saarza9991
@saarza9991 6 ай бұрын
​@@COROLOSTO we all are noobie poobies :)
@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 ^^
@Wakkyguy
@Wakkyguy 4 жыл бұрын
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 жыл бұрын
I have a blog post linked in the description which does a good job of comparing them to each other.
@Wakkyguy
@Wakkyguy 4 жыл бұрын
@@WebDevSimplified Yes I have read that. Just wanted to see them being used in a single project.
@guidoglielmi7992
@guidoglielmi7992 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.
@andrewshaban2888
@andrewshaban2888 Жыл бұрын
You are a good tutor. This is a rare talent. Thank you very much!
@markwillis2336
@markwillis2336 3 жыл бұрын
OooF! This is just pure gold. You just helped me out of a hole again Kyle, thank you so much!
@rahuldwivedi4758
@rahuldwivedi4758 Жыл бұрын
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)
@rileykarl4895
@rileykarl4895 2 жыл бұрын
You and net ninja is a killer combo - thank for the great vids !
@larcho
@larcho Жыл бұрын
Awesome video. There quite a few videos here in youtube that explains this poorly, thanks for this.
@borisnekezov6620
@borisnekezov6620 2 жыл бұрын
Thanks for that video, Kyle! The best and simplest way to understand useCallback!
@mrnabby4178
@mrnabby4178 Жыл бұрын
The topic is crystal clear. Thank you man.
@simeonieroteev4555
@simeonieroteev4555 4 жыл бұрын
That explanation is very, very clear and useful! Thank you!
@vinzbrain
@vinzbrain 3 жыл бұрын
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 3 жыл бұрын
I like the second question, I wonder the same.
@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() !
@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!
@NITESHSINGHNRS
@NITESHSINGHNRS 4 жыл бұрын
bro....your explanation is to diff from others and have more details. keep it up.. superb
@johnhaupenthal
@johnhaupenthal 4 жыл бұрын
Clear and concise, great video.
@sottitron
@sottitron 4 жыл бұрын
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.
@VishalSharma-rn7mt
@VishalSharma-rn7mt 4 жыл бұрын
awesome tutorial, when I will have money, I will definitely buy your course brother
@developerbuddyavikunj5996
@developerbuddyavikunj5996 2 жыл бұрын
It was really helpful 🤗🙏 . Thanks alot for sharing. Finally got clarity on the concept.
@hoyinli7462
@hoyinli7462 2 жыл бұрын
super clear. highly recommend this channel :)
@vivarantx
@vivarantx 2 жыл бұрын
great man, you deserve great success
@xylvnking
@xylvnking 2 жыл бұрын
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
@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
@fabiojonathanarifin1
@fabiojonathanarifin1 2 ай бұрын
very clear explanation, thankyou!
@thebetoxpro
@thebetoxpro 3 жыл бұрын
Amazing explanation. Thank you from Mexico
@keiferramos8858
@keiferramos8858 3 жыл бұрын
very true. You really simplified it.Good Job😌.
@bek3634
@bek3634 Жыл бұрын
really simple explanation! thanks to you a lot.
@johannesperez9548
@johannesperez9548 Жыл бұрын
Crystal Clear, thank you so much!
@tadeuszjiwu255
@tadeuszjiwu255 3 жыл бұрын
This is a very clear explanation, thank you very much, keep up the good work
@logio7663
@logio7663 3 жыл бұрын
Very clear explanation. Thanks a lot ! !
@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 kzbin.info/www/bejne/fZuZaaWEZdt_icU&ab_channel=Front-EndHacks
@cyberwolf9543
@cyberwolf9543 Жыл бұрын
I got this BRO )) THANKS TO YOU!!! AFTER 2 YEARS NOTHING CHANGED ABOUT USECALLBACK BRO?
@ahmadbenchakhtir
@ahmadbenchakhtir 7 ай бұрын
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.
@jaytran247
@jaytran247 4 жыл бұрын
Super genius explanation. Thanks so much.
@maor44
@maor44 3 жыл бұрын
Amazing work man! thank you!
@shizasiddiqui1689
@shizasiddiqui1689 3 жыл бұрын
You're brilliant Kyle.
@kwaku_2023
@kwaku_2023 2 жыл бұрын
You are a blessing!
@shivanyachandra4214
@shivanyachandra4214 4 жыл бұрын
Great video!! can you make a separate based on the difference between useMemo and useCallback, this will really be helpful!
@Karlponken
@Karlponken 2 жыл бұрын
Great explanation! Thanks a lot!
@aryanshmahato
@aryanshmahato 4 жыл бұрын
Desktop in introduction was looking dope! Great video BTW..
@HOLONIA4EVER
@HOLONIA4EVER 4 жыл бұрын
Amazing explanation !
@kose241
@kose241 2 жыл бұрын
you helped me get a job at coinbase, ty
@mayankvora8116
@mayankvora8116 4 жыл бұрын
Awesome Explanation
@csablons
@csablons 4 жыл бұрын
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 3 ай бұрын
This comment needs to be up there, The useCallback() here doesn't prevent a re-render of the child component.
@lakshyaagarwal4044
@lakshyaagarwal4044 4 жыл бұрын
what a wonderful example !!!
@shane2023amazon
@shane2023amazon 3 жыл бұрын
concise and helpful
@derekmoore7401
@derekmoore7401 4 жыл бұрын
very helpful and succinct. Thanks for your videos!
@Neha-sw6ky
@Neha-sw6ky 4 жыл бұрын
Loveeee from India❤❤❤
@thrash_jesus
@thrash_jesus 4 ай бұрын
You're a G man love the vids
@ifillup
@ifillup 4 жыл бұрын
Great explanation (again)!
@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)
@lightrao
@lightrao 2 жыл бұрын
Thank you, very useful !
@luaneaquino7835
@luaneaquino7835 4 жыл бұрын
Thank you! great tutorial
@manishkumartripathi2259
@manishkumartripathi2259 2 жыл бұрын
Nice explanation 😊
@AnoJlJloH
@AnoJlJloH 4 жыл бұрын
You're the BEST!!!
@jefersoncosta2621
@jefersoncosta2621 3 жыл бұрын
awesome!! U rock, thank you so much for this video.
@StephanHaewss
@StephanHaewss 2 жыл бұрын
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 жыл бұрын
@@naveedalirehmani4135 As I understand it, this is equivalent to useCallback, yes.
@jnmldo
@jnmldo Жыл бұрын
Thank you so much for this video
@DediAnanto
@DediAnanto 2 жыл бұрын
Thanks, for beginner like me, I rarely use useCallback if my editor don't suggest it 😄
@sergiyrudenko905
@sergiyrudenko905 3 жыл бұрын
you da best, bro!!! thanks!!!
@eshwarravikanti7659
@eshwarravikanti7659 3 жыл бұрын
Great explanation!!!!! More power to you:)
@iurii7752
@iurii7752 4 жыл бұрын
Great explanation :) Thank you
@lukisIVIII
@lukisIVIII 4 жыл бұрын
You're the best teacher! You have a true gift, thank you!
@davidhahn7391
@davidhahn7391 4 жыл бұрын
great explanation! thank you.
@snarkykat
@snarkykat 4 жыл бұрын
Congratulations on your engagement :)
@Cloud-577
@Cloud-577 4 жыл бұрын
how do you know?
@snarkykat
@snarkykat 4 жыл бұрын
@@Cloud-577 It was on Twitter
@shivashankar6043
@shivashankar6043 3 жыл бұрын
Why don't we use react.memo to prevent rerender in the child component instead of usecallback? Can anyone explain
@chethanprabhu4475
@chethanprabhu4475 2 жыл бұрын
We can use. This is useCallback video so it's for demo purpose only
@theshtig4218
@theshtig4218 Жыл бұрын
The getItems function gets re-created every time the App component runs but wouldn't the same thing happen with the number state? Or state is only created the first time App runs?
@nihadyaqublu2359
@nihadyaqublu2359 2 жыл бұрын
thank you for the video!
@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!😁
@sreekumarmenon
@sreekumarmenon 4 жыл бұрын
thanks, useCallback "simplified"!
@danielluna7648
@danielluna7648 3 жыл бұрын
Can someone please explain where the prevDark is coming from? Am I missing some important knowledge on what we can access in the state?
@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
@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 is 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(){})
@AriRaouf-kq4le
@AriRaouf-kq4le Жыл бұрын
The getItems function return an Array Then in listItem you use useState to returns an array also which you used in iseeffect to retrun the getItems function which also return an array So it become double array? How je used map directly to items?
@verygood5377
@verygood5377 Жыл бұрын
5:32 Callback allows parameter passing
@naserpapi8921
@naserpapi8921 Жыл бұрын
Excellent Thanks
@bibahbibah5108
@bibahbibah5108 2 жыл бұрын
really u have simpleified to me
@sudhansupradhan6891
@sudhansupradhan6891 3 жыл бұрын
Awesome 👌
@ironsand
@ironsand 11 ай бұрын
Is there any use of useCallback other than passing the memoized function in a dependency array? This is the only example I ever see.
@mythm2063
@mythm2063 2 жыл бұрын
Hello i'v got a question, since te console.log is only inside useEffect, So that means the List is still being re-rendered every time ? Maybe you should have used React.memo? Thanks for the potential answer
@viratpatil
@viratpatil 3 жыл бұрын
So using useCallback with getItems will not stop re-rendering of List Component correct ?
@iamprasad88
@iamprasad88 Жыл бұрын
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.
@satindersingh9671
@satindersingh9671 4 жыл бұрын
You are the best
@femaledeer
@femaledeer 4 жыл бұрын
Confusing. My understanding was in useMemo, slowFunction gets called when number changed. useCallback runs a function when number changes. Both execute a function. I don't see the difference.
@thefanbase
@thefanbase 4 жыл бұрын
One executes the function and returns the value to store in that variable and the other one returns the function and stores it in the variable.
Learn Custom Hooks In 10 Minutes
9:38
Web Dev Simplified
Рет қаралды 259 М.
Learn useMemo In 10 Minutes
10:42
Web Dev Simplified
Рет қаралды 493 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 109 М.
Learn useEffect In 13 Minutes
13:38
Web Dev Simplified
Рет қаралды 854 М.
Learn useReducer In 20 Minutes
20:12
Web Dev Simplified
Рет қаралды 520 М.
Зачем на самом деле нужен хук useCallback
8:33
Михаил Непомнящий
Рет қаралды 45 М.
Learn useRef in 11 Minutes
10:20
Web Dev Simplified
Рет қаралды 654 М.
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 859 М.
Speed Up Your React Apps With Code Splitting
16:50
Web Dev Simplified
Рет қаралды 395 М.
Top 6 React Hook Mistakes Beginners Make
21:18
Web Dev Simplified
Рет қаралды 584 М.
REACT MEMO vs USECALLBACK vs USEMEMO
13:18
Coding With Chaim
Рет қаралды 56 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН