#72: useMemo Hook in React 19: Why, When & How to Use It with Practical Examples

Thapa Technical
63 followers
Aug 10, 2024
Hello Whats Up Guys, first of all you
see this example and once you
understand this example then all your doubts will be
cleared. After all what is this use memo hook
and when and why should we use it.
Believe me in this one video you will get the information.
All doubts are going to be cleared,
look carefully at this one component which is currently
visible in front of you, the red box
inside which it is a parent component, okay and
this is one of my parent components, inside this parent
component you can see I have
one.
Component Okay, this is my first component
and this one is my second component,
this is my second component, so now what is
happening, look carefully here, this is my
parent component inside I am
also calling this component, I am my There
is a second component, I am also calling it, I
just want that I have kept a simple
example that whenever I click on this button,
its value for how many times I
clicked gets updated here but
this component here. Isn't it, beyond this
I have done an extensive calculation,
okay and whenever the value of this component
changes, this function will be called and this is an
expensive calculation, so there will be a little delay, so
look carefully at it, and here we have it. This
profile of developer tools, I turn it on,
now I click it once,
after a few milliseconds it clicked
but now I am going to click three times 1
2 three You see guys there is a lack a few
milliseconds
Now I close it also, so the
moment I close it, here you
can see it very clearly, I can see the graph
where this is my pup component,
you can see this is my complete one. There
is an app component which did not render but this
is my memo parent component. Hope now it will be
clear to you. It is very clear. You
can see here, this is my memo parent
container. Inside it,
I had called these two components, out of which this is the parent
component. Why? It is re-rendered as it is said, because there
is a hook inside it, the one with the count is getting re-
rendered,
but this component is
also getting re-rendered along with it, whereas mine is being re-rendered
only from this component. It was related to this and
I had clicked only on this one but
still, this component is being
called again and again, that expensive
function is being called here, you can see this, the one with the
expensive component, see
this. As I told you,
I have named this component as Expensive Component,
so whenever I was doing anything to this parent component,
this component is also being called by default and
also tell the reason for its existence. Why did this
render because the parent component render is
simple? Okay, so now one thing has already come in your mind
that brother, if you did
not want to render this component,
then you can read the react.memo which I gave
you yesterday. It was told in the video that you
could use it. Obviously yes, you
could do this but I would like to tell you that if you use the Use Memo
hook, it will become very easy for you
and you will also know the best example.
Okay, yes, that's why I used the Use Memo hook. If we
should use it, then now let us quickly
read a little about it here,
then we will see it practically.
You must have got the idea that the unnecessarily
expensive calculation is being
called again and again and to calculate it. It was taking
so much time because that
part of the counter had become slow,
so to save it, we use mem hook,
okay, here we are telling
its syntax, so guys, first of all, we will explain a
little bit about it. Let us read the theory, you will get
more ideas, then we will see the practical.
Okay, do watch the video till the end. If you
like the video, then like it with your friends and do
subscribe to Bharbhar's End channel.
So you can see that
when you write, use memo hook. It's kind of like
your a use effect hook only in use effect hook
also when you use memo your a call back
function we need to return something and then we have
one dependency same use effect same
use effect syntax here is simple
and nothing else Ok, you can understand it in that way. The
syntax of use effect is useful in your use memo,
but we are using use memo only so
that we can store the
values of whatever expensive calculations are there
and next
time. If that function is called, then it will
give the same value of CAS. Call the function without
calling the function. I mean, without running the function back, the
simple idea is just this, see, use
memo is a react used form of
memoization, the simple meaning of which is that
any What prop change is happening in the component? What
prop change is happening in this particular component. Memoization means the
same thing. Now this is a technique to
optimize the performance by caching the
result of expansive function
calls. I told you about the expansive function calls.
Now you store the result on the cache,
now whenever someone calls it, you do
not have to run the entire function, you do not have to do all the hard work.
Hey brother, the result is already shown, it is simple, this
is the meaning of use memo, now I hope you understand
and only. This is useful in values, use
memo is useful in optimizing your values
and if you want to optimize the entire function
then you use use call back hook.
This is the difference
if you want to save the function from recreating again and again.
So use call back hook, the value inside that function,
you just have to
store it at one place so that when anyone
calls that function, paste the value from there,
then use memo hook. Simple use memo
is used for. Values Use Call Back Hook is
used for your functions to
save them from getting recreated again and again. Now let's also
see Use it when you want to prevent unnecessary re-
execution of a
function on every render. Here we come. But
there is no problem, now it is useful for
optimizing performance in a situation where
calculations and operations are computationally expensive. Do you understand
what I
showed you? You have already understood and here
also I am telling you not to overuse the unnecessary.
Is it okay with you, okay, now I will
show you it practically and
I have already written the code for you
and the source code is absolutely free, the
link will be found in the description below. Okay,
so till now we have seen a lot, so tomorrow
In the video, I had shown you the function of counting a memo
and basically reading a memo.
Today in this video you can see that
I have created a new file. I did
not use the mjsxj02cm hook because I will try and
tell you as I told you. We have
one our parent component inside that, we have
one component which was your expansive
component A which is this part, okay
and these two parts are inside that,
already we have written it in the normal way,
okay, so now here. This is a
simple counter application, we have one button, okay
and whenever someone clicks on this button,
I am incrementing the value of this count by one
and this is my
paragraph there, I am rendering it. It is
clearly visible but
inside this parent component we have one more
component named Expensive
Component, which I have defined on the same file,
basically you can say, if
you want, you can also keep it on a separate file. You
can create or define a and
import it here. Okay, now
what I have done on this particular component is
I am performing an extensive calculation.
We are calling it extensive
calculation so that
whenever someone calls this particular function. If it does, then
one memory is going to be used and the
second is our CPU, which means it will
take a lot of hard work to calculate it.
Now here we have this expansive
component, okay, there is a function inside it,
that is your sum, you can see. Yes, here
I am calling that function, so
what is happening now, see the problem here, what
is the problem, it is simple that
if I click on this button on this component,
okay, then this is what is its value.
Before live updating,
this component gets re-rendered because
our state is changing here and the
moment the state changed, this is the
particular component, it got called and
when it got called, this is the sum inside it. That
function is called. Now if this
function is called then brother, this is an
expensive calculation that will have to be performed by
our CPU and our
computer. It is taking some time to perform it, so that
little time is being taken.
Right brother, we don't need that,
brother, where is the patience in today's time, you
click a button, it comes after a second, I
mean, after a second, the value is getting updated,
you will go out from there, you
understand that this is fake. So when it is calculated and its
value is returned, I am getting it returned by
updating it again on a constant variable named Total,
then
its value is updated and
visible to you, but now what
should have happened here is this. I should have said that when I
click on this button, it has
nothing to do with me, I should update its value every now and then, so
now
one thing is coming in your mind, that is, in yesterday's video,
I had told about any
component, you know. Which is
calling itself independent and until there is no
props change in that component, do not
disturb it unnecessarily, it is useless, I
mean, don't get it re-rendered, the
simple logic was that if you had
put a memo here. I mean read the memo which
we read yesterday, if you put it here
then it is your job, no, I will
tell you how to see, I am refreshing, I
will go to the end profiler, I am
running it, now I am doing whatever I want.
Click once, see this was not there in the beginning, where was this
in the beginning,
when I clicked three times, it was one to one,
but Route 4 was not shown, at the end,
direct chart was shown and here you
can see it very clearly. Why is it rendered
because the parent is rendered, I mean, a hook has
changed, that's it, you did
not use memo, let's imagine, you did
not use this, I just
told you, and some how it fit in this particular
example, you know, but you know
now, see. What happens, so now how do
we use the memo hook? Here, its
simple function is that it will cache whatever values are there
and
next time if you click on this button, you
know the component, what happened here, update it.
The moment the state variable is updated then it is
re-rendered and the very first
component is the expansive component, this call is made,
he saw, oh baba, this is the same, why should
I do it, why should I spend my efforts, the
values, I know, lying on the cash. Here's
how I picked it up in a chat and
ended it. The job of the use memo is to optimize the value.
You saw what was the job of the react memo in yesterday's video. Okay,
now see what I do. It's simple. Here,
here, I will go. First of all,
if you write use effect at this place, it will also work,
but use memo is fine in this way for syntax,
inside this we have a
callback function, in this way we need to
return something and here we need to pass the
Dependency Array This is your syntax, okay
now what will I do, let me comment this particular code,
okay
whatever values this particular code will return,
I would like to save it on the total
and I have to call this sum function.
I will copy this and this is the
special part when you know the
beauty of fat arrow function is that if it is single
line of code then you can write once I mean you
neither need to write return nor curly
braces simple now whenever you In use, when you
call a hook, you have to return some or the other values. I have used the
function directly here, which
I have returned when I was calling,
so now whatever its value will be, it will be on this page. It
will be saved, okay and here I
have to import it from our
react library, so I have imported the
use memo in this way, it is simple,
at the moment I am not giving any dependency array
because I need any dependency
array here. Now I
do this, I will refresh, I am
going to the profiler again, I
start it, I
clicked once, no problem, I am
going to click three times, see, did
you notice this again? Look, what I mean to say is
that now with the help of the use memo hook,
see this here. Give it
a second. If I click on this here too, then yes,
okay, no problem. Look at
this here, you can see it very clearly.
Be that as it may, if it is re-rendered, it will be re-
rendered, no problem, okay, but
you can see the duration, it is even less than
0.1 millisecond, the duration has become so
less now because whatever value is there,
how can it be directly From this we are getting
and just finished like this is your actual
right can you see this is your actual
and this is your special part
where like now there is no tension now very
easy like this is your special function now
We did that optimization with the help
of ya,
see how did you guys like the video, I hope the
video was a little long but I think you
understood that the work of use memo hook is any
value, it is ok to optimize it
so or this is It Guy, that's all I hope for this video today.
You must have liked the video very
much and the doubt about the Use Memo
hook must have been cleared.
One More Time Use Memo's function is to
optimize the performance by catching the
results of An Expensive Function Call: It
caches the values of the expansive function calls and next
time if the call is made, it pastes those values there
without having to perform that operation
again because now it
knows something. If there is no change, then
its output will be the same, why
perform it? Give that value directly,
then here your use memo comes in handy. I know,
now there is definitely a doubt in your mind and
that's it, I used that react dot memo.
Which was told in yesterday's video, whose
job is to save any component
from being rendered unnecessarily. And
in the beginning, I have solved this particular puff, I mean, which was
our today's video,
through memo. I had
directly told you
not to render this component. When the component itself will not be rendered
then it would be impossible to run the function inside it.
But with the help of use memo hook,
run the component but
optimize the values of the function. Okay and
guys one more thing, I was going to share two examples
with you in this video today
but I guess somewhere this
part also got a little long so
I will tell you that in tomorrow's video.
Like memo is fine, but
sometimes some situation will arise where
even if you just write memo, it will
not work for you, you will have to use memo hook
only, so what is that situation,
I will tell you about it tomorrow. I will tell you in the video
and the day after tomorrow's video, use call back hook and
your work is over, like so many hooks, you have
understood, if you have understood then it is more than enough and as for the
compiler, we will definitely see
that, do not worry at all, but read
memo use. It is important to understand memo and use tomorrow back
even if it is 2024 no problem you know
because the old projects are still
running on the same so guys this is very
important so guys if you have any
other best example regarding use memo
let Know me in the comment section, I will be very
happy, it will help other people too. If you
like the video, please
share it with your friends and the next
target is 7 lakh subscribers. If you have not
done it yet, please do so. I will meet
you tomorrow. In the video of 'Til The Guys Thank
You So Much Take Care
[Music]
