Learn React Hooks: useMemo - Simply Explained!

  Рет қаралды 79,622

Cosden Solutions

Cosden Solutions

Жыл бұрын

Join The Discord! → discord.cosdensolutions.io
Source Code → github.com/cosdensolutions/co...
In this video we will learn about React hooks, starting with useMemo. This powerful React hook will allow you to fix your performance problems by memoizing a value and only re-computing when necessary. You will learn how to identify expensive computations in your components, how they affect the performance of your React app, and how to implement React's useMemo to memoize the value and use the dependency array to only compute when necessary.
In this new React world, hooks are here to stay, so it's best to learn them! In this tutorial I demonstrate the useMemo React hook, and I explain it very simply and in a way that is easy to understand. Enjoy!

Пікірлер: 199
@cosdensolutions
@cosdensolutions 3 ай бұрын
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
@EnesKab
@EnesKab 2 ай бұрын
Please add also NextJS to the Project React Course 🙂
@abhishekprajapati6849
@abhishekprajapati6849 6 ай бұрын
This was my first and the last useMemo tutorial. I just loved the way you isolated the concept along with a really simple explanation. Thank you!
@areebafatima7436
@areebafatima7436 7 ай бұрын
I am so happy that I got landed to this channel. So concise, loving it.
@kacperstepien8640
@kacperstepien8640 6 ай бұрын
The best useMemo explanation I've seen.
@sahasrabhujaneuron5975
@sahasrabhujaneuron5975 11 ай бұрын
I've been struggling with this concept, but this video really made it easy and simple to understand. Many thanks!
@cosdensolutions
@cosdensolutions 11 ай бұрын
glad it helped! 🤙
@robboerman9378
@robboerman9378 29 күн бұрын
After watching 3 of your videos I have learned more than reading 3 books on React. Your videos are exceptionally clear. Thanks
@mostinho7
@mostinho7 4 ай бұрын
Done thanks clear 6:50 useMemo memoizes the result of a calculation/ function call until some dependencies change
@shohanredwan
@shohanredwan Жыл бұрын
Wow... perfectly explained man, Thanks.
@HerlonCosta
@HerlonCosta 4 ай бұрын
It's definitely the best explanation I've ever seen. Thank you very much!
@smrpkrl
@smrpkrl 7 ай бұрын
thank god i stumbled upon your channel..i am learning about all the hooks that react has to offer through your channel..i am loving the clear explanations that you provide..not a single dull moment in the video! kudos to you sir! love you!!!
@uquantum
@uquantum 9 ай бұрын
Really useful tutorial. Thanks for being so helpful!
@fizdanielz
@fizdanielz 9 ай бұрын
You have the best tutorials channel 🙌🙌🙌🙌
@allan_gr
@allan_gr 9 ай бұрын
Simply explained indeed, Thank you.
@mehdiSupp
@mehdiSupp 4 ай бұрын
Timing the "like and subscribe" popup exactly after you explain the solution is genius. I clicked like and it felt good I don't know why lol
@harshitkrvishwakarma3602
@harshitkrvishwakarma3602 6 ай бұрын
Love your explanations, you have made react a lot simpler.
@RichReflectionz
@RichReflectionz Жыл бұрын
As someone still new to react and web dev in general, this is awesome, the main hooks I’ve been using have been useState and useEffect. Would be great to see one about useRef too :)
@cosdensolutions
@cosdensolutions Жыл бұрын
It's coming! Just started this 😁
@badenis1291
@badenis1291 5 ай бұрын
Made me rethink the way I was seeing this hook. Thanks
@LauraDuranVelazquez-md4ot
@LauraDuranVelazquez-md4ot 18 күн бұрын
Thank you for explaining these concepts so simply! Love your videos.
@Ksahdia
@Ksahdia 9 ай бұрын
Thank you very much for the easier explained concepts. I'm watching your videos to understand my lessons better. Both compliment each other well.
@cosdensolutions
@cosdensolutions 9 ай бұрын
wonderful!
@user-rs9pc1cu7v
@user-rs9pc1cu7v 8 ай бұрын
So good! You explained it perfectly!
@netaivgi
@netaivgi 11 күн бұрын
So simply explained! wow, amazing
@user-me6tr8ko6m
@user-me6tr8ko6m 3 ай бұрын
you are THE BEST to explain!
@user-lg2xl1ik9k
@user-lg2xl1ik9k 3 ай бұрын
the best video explains useMemo!! thank you so much!
@pragatijadhav26
@pragatijadhav26 3 ай бұрын
Best explanation ever. understand in one go.
@DarkRider213
@DarkRider213 4 ай бұрын
this was a perfect explanation - thank you!
@harag9
@harag9 7 ай бұрын
Great video, thanks for this, I'll have to look at implementing this now.
@codetechpro
@codetechpro Ай бұрын
that was very helpful, thank you for clearing the confusion
@jaykaytech
@jaykaytech 4 ай бұрын
the most clear explanation!
@LowriKate
@LowriKate 11 ай бұрын
Great explanation, thank you!
@WaseemAhmad-mf3wh
@WaseemAhmad-mf3wh 3 ай бұрын
Thank you for your clear explanation ❤
@BBocho
@BBocho 7 ай бұрын
great explanation, thank you very much!
@coolme7437
@coolme7437 5 ай бұрын
Best tutorial for useMemo!
@amarmusovic7748
@amarmusovic7748 2 ай бұрын
This could be the best useMemo tutorial I've seen. And I've seen a lot. Almost too many. 😂 Oh, and I love that you keep rounding 29.999.999 to 29 million. Made me chuckle every time... 🤗🤗
@dharylcarryalmora5414
@dharylcarryalmora5414 4 ай бұрын
I like your tutorial very short and will explain. Thank you so much 😊😊
@dianprogrammer
@dianprogrammer Жыл бұрын
Great video I just begin my react js journey and can easily understand the concept
@2029leandro
@2029leandro 3 ай бұрын
This guy is the best teaching react stuff
@RickCodez
@RickCodez 9 ай бұрын
Very well explained!
@martrom0
@martrom0 2 ай бұрын
Wonderful !! 👏 I subscribe right away !
@reactdev2838
@reactdev2838 6 ай бұрын
Once again great tutorial...
@Viva-07
@Viva-07 4 ай бұрын
Awesome...very well explained
@bhargavchadalla
@bhargavchadalla Жыл бұрын
Thank you, great explanation
@rood4449
@rood4449 11 ай бұрын
that is the perfect fundamentals . it was very efficient to understand the how important useMemo . thx :)
@cosdensolutions
@cosdensolutions 11 ай бұрын
Glad you found it useful! 🤙
@oscardasilva971
@oscardasilva971 Жыл бұрын
Well explained Thank you very much.
@athmaram4256
@athmaram4256 8 ай бұрын
Great explication bro. Thank you very much
@hoangtran-ek8mn
@hoangtran-ek8mn 9 ай бұрын
Ohhh mannn. Why are you so good at teaching? Thank you so much!!!
@cosdensolutions
@cosdensolutions 9 ай бұрын
You're most welcome ☺️
@adiletbaimyrzauulu6589
@adiletbaimyrzauulu6589 8 ай бұрын
Hey, i really like your way of explaining. I bought an online course on React, but sometimes I just don't get it on the course, and your videos make it super duper clear for me. Thank you for your job, wish you all the best, and I hope some day you are going to have 500k subs on youtube.
@cosdensolutions
@cosdensolutions 8 ай бұрын
thank you for the kind words! I'm also working on a course by the way, will have this teaching style and a really well thought out plan that I haven't seen done in another course before! Coming in a couple of months
@user-dq1to7hg2n
@user-dq1to7hg2n Ай бұрын
Great explanation!
@agosolsgaming4036
@agosolsgaming4036 3 ай бұрын
awesome explanation, thanks
@nawazishali274
@nawazishali274 10 ай бұрын
Ohh man nicely explained 🎉❤
@varnerdamasceno8093
@varnerdamasceno8093 17 күн бұрын
This video helped me a lot.
@gharbos
@gharbos Жыл бұрын
Appreciate your explanation ! thanx
@samahgad241
@samahgad241 6 ай бұрын
thanks alot , very helpful🌹
@jitujahagirdar613
@jitujahagirdar613 4 ай бұрын
Thanks for this video 👍👍👍 really good way that you explain . I am lazy person who understand only if simplify way to explain of context and you did it 😜🤗. Good job..!😊
@mandardeshpande7976
@mandardeshpande7976 8 ай бұрын
No one has ever articulated this topic so well. Because it's simple yet confusing. Also I was tired of watching premium full blown course about shopping cart app with tailwind and what not, all to explain the few key concepts hardly for 5 mins which eventually gets buried somewhere in the middle. Thanks for the short and sweet content.
@PriyaToliwal
@PriyaToliwal 6 ай бұрын
Hi, your way of teaching is very easy to understand and helps me learn more concepts. Please make a video on Reactjs Forms as well.. it would really help me understand forms better. Thanks for making learning so easy and effective.
@arek9430
@arek9430 6 ай бұрын
Good stuff, thanks
@OXIDE777-is6gs
@OXIDE777-is6gs Жыл бұрын
Great one!! thanks bro
@SWE507
@SWE507 3 ай бұрын
Im enrolled in a couple of intermediate and advanced react courses.. even bought a couple of books.. But man, your vids are waaay better and so amazing! clear and concise!!!! i wish you had a full on intermediate/advanced react course. i would gladly pay for that! Thank you so much for existing!!!
@cosdensolutions
@cosdensolutions 3 ай бұрын
I'm just about to launch a course that has 2 parts. The first part is more beginner, the second is more intermediate/advanced ☺️ if you get it when it launches you get both for the price of one
@SWE507
@SWE507 3 ай бұрын
Will be sure to do so!! thank you! :D @@cosdensolutions
@pankajchaudhary_281
@pankajchaudhary_281 Жыл бұрын
Best video ever❤❤
@abhinavdhama3014
@abhinavdhama3014 7 ай бұрын
Amazing amazing amazing explanation bro . ♥️♥️♥️♥️♥️. Plss do a javascript series also
@mathemagicstelugu7848
@mathemagicstelugu7848 5 ай бұрын
Really its a Last Usememo video for me🥰🥰🥰
@isaac-9185
@isaac-9185 9 ай бұрын
Really well explained, thank you. Also mind dropping a list of most common cases that you would use useMemo for?
@cosdensolutions
@cosdensolutions 9 ай бұрын
I would only use it when there's an expensive computation to make, or when I want to prevent this from causing a expensive child component to re-render
@user-pu5eh6mm6y
@user-pu5eh6mm6y 7 ай бұрын
dude you're awesome. Thank you so much for your explanations, now I guess I don't need to watch other videos about react hooks :)
@cosdensolutions
@cosdensolutions 7 ай бұрын
☺️
@dstrader7583
@dstrader7583 11 ай бұрын
Thanks, Alot Man. Glad i picked your video first now i don't need to watch another video on this.
@cosdensolutions
@cosdensolutions 11 ай бұрын
🤙
@romanzaritskyi303
@romanzaritskyi303 3 ай бұрын
Thanks, man
@MuhammadRidwan-kq4yw
@MuhammadRidwan-kq4yw 5 күн бұрын
youre an react angel haha,awesome bro,thank you
@sudhanshusharma9888
@sudhanshusharma9888 Жыл бұрын
Well explained
@user-mr8rv8yb8v
@user-mr8rv8yb8v Ай бұрын
best teaching
@uzairahmed2975
@uzairahmed2975 4 ай бұрын
Love it the way you explained everything single detail, Can I also use useMemo with autocomplete with fetch?
@cosdensolutions
@cosdensolutions 4 ай бұрын
yeah you can!
@redhood7105
@redhood7105 Ай бұрын
God tier video
@iamparmjeetmishra
@iamparmjeetmishra Ай бұрын
thank you so much
@user-se7fr3pt8s
@user-se7fr3pt8s Ай бұрын
Thanks man
@jackmackenzie8466
@jackmackenzie8466 11 күн бұрын
Great videos. 1 question though, when you have count and items in the hook, why doesn't that lag as it seems like the items are being looped each time count changes?
@akrm3240
@akrm3240 8 ай бұрын
Thanks a lot!
@AyushGupta-kp9xf
@AyushGupta-kp9xf 2 ай бұрын
Nice explanation, appreciate that. Btw dude how do you spam that fast!
@thatkannurgirl
@thatkannurgirl 4 ай бұрын
Thank you
@Vishal360
@Vishal360 4 ай бұрын
every tutorial of cosden solution is last tutorial ur gonna watch - Fr ✌✌
@BushidoWegDesKrieger
@BushidoWegDesKrieger 4 ай бұрын
I reviewed some custom hooks and sometimes found useCallback/useMemo with an empty dependency array. Could you explain why that is?
@gionniilbassotto
@gionniilbassotto 4 ай бұрын
so useMemo is fairly similar to useEffect? but useEffect is called when the page is loaded and/or for some dependencies?
@dharmeshgohil9375
@dharmeshgohil9375 6 ай бұрын
all your video is awesome keep it up ,make small project which make use of the all hooks how to use in company level or share some difficult situation of it when to use which hooks use it will be great help
@cosdensolutions
@cosdensolutions 6 ай бұрын
Currently in the process of making a full on course with exactly that!
@balls991
@balls991 Жыл бұрын
Awesome explanation, thank you. Could you provide the name of the theme you are using too?
@cosdensolutions
@cosdensolutions Жыл бұрын
Material theme darker! 🤙
@2ameridiem
@2ameridiem Ай бұрын
What is the difference between useMemo and useEffect? If we wrap the selectedItem function inside a useEffect like- useEffect( ( )=>{ const selectedItem=items.find((item)=>item.isSelected) },[items ] ) then it's serving the same purpose.
@emmanuelareiza9699
@emmanuelareiza9699 Ай бұрын
Ty bro
@TorreLoCascio-mq1bx
@TorreLoCascio-mq1bx 2 ай бұрын
How does React know if "items' changes with looping through all 29mil items? Wouldn't that be the same performance issue we had? Thanks, great tutorial in general.
@Code_with_random
@Code_with_random 20 күн бұрын
Can we use multiple use effect in one component
@jasonliu6321
@jasonliu6321 6 ай бұрын
Hi Cosden, I do not figure out why do we need to use useState to wrap the initialItems. Does it make much sense?
@cosdensolutions
@cosdensolutions 6 ай бұрын
Because the items can change over time and for that you need state!
@AlvaroDoune
@AlvaroDoune 4 ай бұрын
Why is faster compare if the items array change than find function?
@dariotizzoni1517
@dariotizzoni1517 8 ай бұрын
Hi, I’m new to react and your videos are really helpful. I have two questions: 1. In the last specific scenario, is it really necessary using useMemo? I mean, the only cause of rerender is when count changes, so useMemo would compare every render the dependecies. 2. What is the differences between useMemo and useCallback?
@cosdensolutions
@cosdensolutions 8 ай бұрын
1. In the context of the video, no it isn't. But the goal was to show you how it works! 2. useMemo returns a value, useCallback returns a function. They are the same otherwise!
@dariotizzoni1517
@dariotizzoni1517 8 ай бұрын
Oh, that’s right! Really thanks 😊
@jritzeku
@jritzeku 8 ай бұрын
This hooks series are great!! I had a question.Is there a way to gauge when to use useMemo vs not? In our case it made sense simply because of the sample size of array being over 29million but realistically it's hard to determine this intuitively...or even with our simply array at what size would be the cut off(to use/not use useMemo). I guess this question is applicable to useCallback too.
@cosdensolutions
@cosdensolutions 8 ай бұрын
the general idea is to do it whenever you're mapping over something or computing something expensive. If you have that, you generally want to wrap that in useMemo so that it doesn't unnecessarily run every time. The more you work with it, the more experience you have and you learn to intuitively know when and when not to use it
@user-th4cm8wr1p
@user-th4cm8wr1p 7 ай бұрын
Hi sir, I am grateful that I landed here and learn some valuable concepts of useMemo, I have a question that on 11:30 minute you added the count as a dependency array, now when a count changes that computation will run again, then how the usememo is beneficial here, let me know please?
@cosdensolutions
@cosdensolutions 7 ай бұрын
The useMemo is not to prevent a re-render, but rather control when it happens. In that example we did indeed invalidate what we did by adding count, however that's necessary. Our memo depended on count so we had to provide it. The benefit lies when we add a new state variable that is not related to that, then useMemo won't re-render when that changes. So again, we're not trying to prevent a re-render, but rather control when it happens and only have it happen when absolutely necessary, otherwise return the memoed value
@3042640426
@3042640426 Ай бұрын
In this case, if the count changes frequently and the re-render is only triggered by it, using useMemo here would be unnecessary. Right?
@krzysztofrozbicki1776
@krzysztofrozbicki1776 6 ай бұрын
I really like your videos - they are very informative and educational. I have only one question - what if we know that items won`t change (e.g. these are the static backend property). Could we define the selectedItem outside the component and then use it. e.g.: import { initialItems } from './utils'; const selectedItem = initialItems.find(item => item.isSelected); functionDemo({}: DemoProps){ //Rest of code without any of useState and useMemo hooks for items } It will work and the find will run only once, (when refreshing the website and rendering the full component); I know that it would be working only in edge case when we are 100% sure that neither initialItems nor item.isSelected will never change on this component and is static. Could you explain to me why it would be considered bad practice.
@cosdensolutions
@cosdensolutions 6 ай бұрын
It wouldn't be bad practice but in your example your value is static, not from the backend. If it came from the backend you'd have to fetch it in a component and then you need to do what I did in the video. But yes if it's just a random static variable in some file, sure you can do what you did. Even put selectedItem in that same file too and just import it
@rajantwanabashu
@rajantwanabashu Ай бұрын
I don't understand how is it optimizing performance when using const selectedItem = useMemo( () => items.find((item) => item.id === count), [count, items], ); when I only have count and items state in component. any how whenever one of them change selectedItem recomputes.
@buildsome
@buildsome 4 ай бұрын
What will be the impact if we use useEffect in this example with selectedItem as another state? Apart from useMemo memoising the result. I am asking in this specific example?
@cosdensolutions
@cosdensolutions 4 ай бұрын
It would be an extra state and useEffect to manage, plus another render cycle. It's always best to derive state whenever possible
@yuongeon1476
@yuongeon1476 6 ай бұрын
Thanks! That's useful for me. And one ques it that may have some diff between use "const [item] = useState(initial)" and "const item = useRef(initial)" in ur case?
@cosdensolutions
@cosdensolutions 6 ай бұрын
yeah there is a difference, check out my video on useRef to really understand!
@yuongeon1476
@yuongeon1476 6 ай бұрын
@@cosdensolutions Thank for that, and can u make a video about Memo (not useMemo) cause i wonder when to use it
@cosdensolutions
@cosdensolutions 6 ай бұрын
@@yuongeon1476 check out useCallback, I show it there!
@SachinMeena-iv4hw
@SachinMeena-iv4hw 29 күн бұрын
so what is the difference between useEffect and useMemo , by looking this example I felt we could do the same thing with useEffect , cant we ??
@AbsolutePain
@AbsolutePain 5 ай бұрын
Hey cosden, we can achieve the similar results using useEffect ? so how do we identify when should we use memo ? // Use effect equivalent: import { useState, useMemo, useEffect } from "react"; import { data } from "./utils/data"; const Counter = () => { const [arr] = useState(data); const [selectedValue, setSelectedValue] = useState(0); useEffect(() => { const val = arr.find((elem) => elem.isSelected); setSelectedValue(val); }, [arr]); const [count, setCount] = useState(0); return ( Count:{count} setCount(count + 1)}>+1 selectedValue : {selectedValue.id} ); }; export default Counter; Thnaks for the video. Can you make a complete project in react with best practices. Thanks.
@cosdensolutions
@cosdensolutions 5 ай бұрын
So while your example works, it's not the recommended approach. The way you have it, you have an extra state variable that you have to manage and a whole useEffect to manage it. If you do it like I showed in useMemo, then selectedValue automatically gets re-computed when the array changes, and you don't have to do it yourself. That's why it's a better approach. There are a lot of times where something shouldn't be done with an useEffect even if it can, just because it introduces an extra dependency you have to manage and keep up to date. Also, your example creates an extra render cycle in the component which is unnecessary
@simplysxb
@simplysxb 23 күн бұрын
Hello their, your video is just clear the useEffect. I copied your code and observed it is showing The couunt is 0 THEN I am been cleand-up THEN The couunt is 0 on refresh the page. Why so?
@mirkafarkasova2425
@mirkafarkasova2425 2 ай бұрын
Great but cant I use useEffect instead? like it also has the dependencies array, and only runs once guaranteed if items dont change.
@cosdensolutions
@cosdensolutions 2 ай бұрын
You could, but it's not recommended. Using useEffect is adding an extra dependency that you have to manage. Using useMemo is automatically handling it for you without an extra render update
@samarthchaplot2998
@samarthchaplot2998 3 күн бұрын
Can't we accomplish the same thing using useEffect? Can someone explain the difference of using these two hooks in this scenario?
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 73 М.
You Probably Shouldn't Use React.memo()
10:17
Theo - t3․gg
Рет қаралды 53 М.
Пробую самое сладкое вещество во Вселенной
00:41
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 2,4 МЛН
Её Старший Брат Настоящий Джентельмен ❤️
00:18
Глеб Рандалайнен
Рет қаралды 8 МЛН
Learn React Hooks: useRef - Simply Explained!
12:42
Cosden Solutions
Рет қаралды 80 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Learn React Hooks: useContext - Simply Explained!
15:46
Cosden Solutions
Рет қаралды 128 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 455 М.
Learn React Hooks: useLayoutEffect - Simply Explained!
9:17
Cosden Solutions
Рет қаралды 15 М.
Learn React Hooks: useEffect - Simply Explained!
14:07
Cosden Solutions
Рет қаралды 115 М.
WTF Do These Even Mean
13:44
Web Dev Simplified
Рет қаралды 75 М.
You might not need useEffect() ...
21:45
Academind
Рет қаралды 151 М.
Mastering React Memo
26:56
Jack Herrington
Рет қаралды 132 М.