Did React 19 really make useMemo, useCallback and React.memo useless? That's what a lot of posts and tutorials claim, and it's only half true. React 19 itself didn't change how re-rendering works. Unless you turn on the React Compiler, your React 19 app re-renders exactly like a React 18 app, and you still need to know these hooks.
In this video, we build a small demo app and watch the problem happen live. A simple state change makes the parent re-render, a child component re-renders with it, and an expensive function runs again on every click, even when nothing it depends on has changed.
Then we fix it step by step. You'll see how useMemo caches an expensive calculation so it only runs when its inputs change. Next, we find out why a child component can still re-render even after that fix: functions get a new reference on every render, so React thinks the props changed. Finally, we stabilize that function with useCallback and stop the extra renders.
By the end, you'll know exactly what each hook does, when it's worth using, and why React 19 alone doesn't remove the need for them. In Part 2, we'll turn on the React Compiler and delete these hooks, with proof that it works.
⌛TIMELINE⏳ 0:00 The React 19 Myth 1:15 Our Demo App 2:15 Join the Live React Class 3:00 Code Walkthrough 4:58 The Child Component 7:10 The Expensive Function Problem 10:25 Fixing It with useMemo 12:16 Why Does the Child Still Re-render? 14:10 Stabilizing Functions with useCallback
AI Generated·May contain errors
Video Summary
AI Generated
This video clarifies that React 19 still requires manual optimization via `useMemo`, `useCallback`, and `React.memo` unless the optional React Compiler is used. It demonstrates how to prevent unnecessary re-renders caused by expensive calculations and the recreation of function objects, ensuring better application performance through referential equality.
Hey guys, in the world of React, you must have heard this line or read it, and that is that
React version 19 has arrived, so now we no longer need useMemo, useCallback, or memo.
But if I tell you the truth, then that is only half the truth, essentially half the truth.
Because in React 19, no rules have been changed regarding re-rendering.
Yes, guys, there's no doubt you know that we now have a React compiler that does automatic memoization.
But the reality is that this is also an opt-in build plugin, and you must enable the compiler.
If you don't enable the compiler in React 19, your React 19 application will re-render exactly as it does in React 18.
Yes, guys, so in this video, we'll once again dive into understanding our OGs: useMemo, useCallback, and React.memo.
And in tomorrow's video, I'll tell you where you can expect the automatic memoization from the React compiler.
But in today's video, I'm going to tell you a lot of things you didn't know until now, and you must be thinking that
Now the React compiler will handle everything, I don't need it anymore, but in reality, it still works and can still be used in certain places.
So watch this video till the end, guys, a lot of your concepts will be clear today.
And tomorrow, the React compiler will be fun.
Alright, so you can see that this is our demo example today, where everything will be clear to you.
There is an increment button that, as soon as I click it, increases from one to the next.
And you can see the filtering happening here; now you know, it tells you how much time it took, basically.
Then if you type something here, like what I wrote P, you can see that your product list is being re-rendered.
Basically rendered, even here too.
So this is your child component, which means that even if you increment or modify something here,
Your child component is being called.
But in this video, I will tell you that when I click on it, I don't need it.
So, guys, you've got to know a lot of things here.
In fact, the filter you can see, I don't need it either.
So now, how can we address it?
How can we optimize it?
I'll tell you that, but before that, guys, if you want to learn React live from me,
Then I'm taking React classes every night at nine o'clock.
Alright, there are live classes via Google Meet.
And the price I have kept is only Rs. 1,699.
This is for two more days, that's it.
After that, its price will be Rs. 9,999 again, guys.
So if you're interested, I'll provide its link in the description and the comment section below.
Please write your name, email, and mobile number here,
Pay this amount and unroll it.
I will communicate with you on WhatsApp, add you to the group.
And then we will study together at night.
Alright, I'll take you straight to our code.
I will tell you simply where the problem is, and then how we can fix it.
So, guys, our code looks something like this.
And here we have this optimization function.
It's very simple, very basic.
Alright, now look at it carefully.
Let's understand the return one by one.
There is an H1.
There is a state variable here, count.
There is a button.
I will increment the set count from 1 to 1.
Simple increment.
So basically, the set count is obtained from here, from the use state.
This is your setter function.
So whenever you click this button,
We will increment the value of count from 1 to 1.
So what will happen?
The value of count will be updated.
Now, if it is updated,
Obviously, it has to be updated.
Then your optimization function will be re-rendered.
When it is re-rendered,
It will work from top to bottom.
So you can clearly see here,
this filtered function,
basically,
a constant expression
That will be up to you.
And that's why you can see this filter.
And how much time is it taking?
I have shown you through console.timeEnd.
It's up to you.
And you can see here,
There is one more function, handleSelect.
What is this?
Again, it is an expression.
And we all know that
A function object has been created in our heap.
So on every re-render,
A function object is being created.
I hope you have understood.
What the problem is.
I am clicking on this button
so that I can increment
The value of my count from 1.
And it is happening.
But in the meantime,
because our component is being re-rendered,
our filter
We're calling it again.
The filter is running again.
And our function,
basically,
The function object is being created again.
Isn't it?
And if you see,
in this return
You also have a child component.
And that's your product list.
And if you look closely,
in this product list,
I Noble have written rendered.
It means that
not only the count,
but also the child component
is being called.
This is too much.
This was happening.
But why do we have to call the child component?
We have to call it.
only when the value
has been changed.
I don't have to call it.
Now, we have to understand
these three problems
The filter method is running over and over again.
Our function is being created over and over again.
And the child component
is being called over and over again.
How to deal with it?
How to optimize it?
First of all,
We have to see that
I want my filter function
to be called only when
Someone is typing
Anything on this input field?
to search
We are doing the same on the filter.
By the way, where did this come from?
Where does the product come from?
Its symbol means that
if you want 5,000 fake products,
5,000 or 50,000
if you want counterfeit products,
basically fake product items,
Then we can easily create them.
with the help of Array.from
without using a for loop
Let me show you.
the products
Let me show you.
50,000
array of an object
Can you see it?
You get it on the first element
zero index number
id zero, name product zero
Price one.
Nothing else.
We've looped it.
The user who is typing
dot includes
That name has been converted.
to lower case
That's it.
Now,
We want that.
when someone is typing on the input,
your filter method
should work or not
For the very first time,
It will work, but after that,
It shouldn't work.
When someone is typing,
It should work.
This is my product list child component.
When the value of these props
If changes are made, then it should work or not.
For the first time
It will work, but it shouldn't.
Now, let's address them one by one.
First of all, let's tackle this
Filter. Now, look carefully.
What do we get here.
I am refreshing. I am removing this.
If I click on this,
I am removing this product.
Look carefully.
I am removing this.
I'm clicking on this.
In the filter, I'm getting so many milliseconds
And I can see my child component.
I don't want this.
When I am incrementing,
Why is my filter working?
Why is my method being called?
I don't want this.
Now, to optimize this,
We will use the use
memo hook
What will the use memo hook do?
It will be cached.
the result
If it checks
and when you click the button,
It is re-rendered.
It has been cached once.
When it comes back
on this line
It will see that
There is no change in value here.
We have already stored
the value in the cache
When the second
When it re-renders, it will see
that the value is the same.
Why do we have to run it again?
The value in the cache is returned.
At the very least,
We don't have to run it again.
Yes.
That's it.
If you want to control
such expensive results,
We use the memo.
hook
It will be cached.
your values
If we search something in the future,
Only then will it call.
Otherwise,
You will get the value
Yes.
Let's say you are solving
A math problem.
It took you half an hour.
to solve it
It took you 30 minutes.
to solve it.
You had to go very deep
to solve it.
Now, another person comes
and asks the same question
with the same input value.
You know the answer.
is the same, but you have to
Work hard for 30 minutes
To get it.
The third person also asked the same question.
You know it will take me 30 minutes.
to get the same output.
If we want to optimize
or memoize it
What do we do?
Whatever the answer is,
at the end,
whatever the value is,
You have saved it
in one place
Now, the third person
asks the same question.
It took you 30 minutes.
to solve it.
If you are doing
an expensive calculation
if you know that the value
is going to be the same,
Why do you have to do it over and over again?
There, your usage memo comes in handy.
You don't have to do anything.
By the way,
The syntax of use memo is something like this.
Use memo
Is your callback?
and then your array dependency.
This is your usage memo.
You can see
the products of Ditto
You have to pick it up
and put it
Inside it.
Got it?
Now, pick it up
and put it here.
By the way,
You are dependent on the query.
You have to write a query
Here.
Now, I will write
this filter
Inside it.
Let's write it like this.
Now, look carefully.
A while ago,
This filter was being called over and over again.
Now, look carefully.
You cannot read properties of undefined
product list
By the way, let me tell you that
You have to come back here.
Rather than return,
you can write
Expensive outcome.
Expensive outcome.
Now,
It's like this.
Now, I will show you
When you write, use memo.
Now, you can see it.
But, look carefully.
Now, you cannot see
the filter
What does it mean?
It means that no matter how much you
increment
your filter method
will not be called.
Here, I have solved an optimization problem.
Problem with the help of use memo.
Now, it will not be called.
It will be called only
when you
See here.
When we change the query,
Only then your product.filter
The function will work.
Now, let me tell you the second one.
The second one is
the child component that is called every time
You can see here
by rendering the product list.
Basically, that's it.
I don't need this.
If I am doing something about it,
Then why should I print it over and over again?
It is already there.
Rather than fetching and printing it again,
Let it be.
So, here's what we'll do about it.
If you want to optimize
any component,
for that
We use memo.
Either React.memo or simple.memo.
You have to wrap it.
in simple.memo
Remove all of this and put it in the memo.
This is it.
That's all you have to do.
Obviously, you have to make a face
Here.
Make the product list like this.
Your work is done.
That's it.
The work of memo is
if you want to optimize
Any component, then use memo.
If you want to optimize values,
Then use memo.
So, I went like this.
By the way, you can see the memo at the top.
We've got it through React.
Let me save the controller.
Let me refresh it.
You're still getting it.
Why am I getting it?
I have already written
What I wanted to optimize it.
I have written it in the memo.
Still, I'm getting it.
Now, the most important thing
that you must keep in mind
and that is
Your child component only works
when the props inside it change.
Right?
If we talk about filtered,
If we talk about products,
This has been filtered, and I have optimized it.
Right?
There is no problem with it.
The problem is
with this function.
That's why handleSelect.
What did I tell you?
For the very first time,
when the function was called,
It came from the top.
I have optimized it.
Initially,
the filter method
was called
And you got the results.
Right?
Now,
when it is ready,
then
this function,
Actually, we have a heap.
and there
It is created.
and sits somewhere.
Basically, this function
gets a place in our heap, memory.
Now,
this handleSelect
is on your call stack.
We will keep it on the call stack.
Right?
This is your handleSelect.
This handleSelect
has a function,
handleSelect,
It has a memory address.
For the first time.
Now, listen carefully.
At first, there was no problem.
Now, I clicked on this button.
When I clicked on this button,
The count value increased.
My state variable has been updated.
When it was updated,
The optimization function was called again.
I mean, "called" means
re-rendered
When it was re-rendered,
when it reached this line
There was no change in the query.
So, it returned the value as it is.
But,
after re-render
when it came back to this line
We haven't done anything.
to handleSelect.
Again,
a new function object
has been created
A new function object has been created.
Once again,
handleSelect
has a new memory address.
Listen carefully.
It has a new memory address.
Now, to optimize
our child component,
We used a memo.
We used a memo.
Do you know what memo checks?
It checks the props.
It checks whether there has been any change in the props.
There was no problem with the filtered one.
because we used a memo.
But, the handleSelect
The function is the same.
There is no state variable here.
Still,
when we use
handleSelect for the first time
and after re-render,
the handleSelect
is checked inside the DOM.
You must have heard about the diffing algorithm.
Your old vs new DOM tree
Checks the difference.
It verified that
handle1
handleSelect function
has the address
of this one.
After re-render,
the handleSelect function
Do you have the address for this one?
It said that these two are different.
So, memo always uses
triple equals to check
It verified that
The memory address is different.
It means something has changed here.
Now, if this has changed,
if this has changed,
Then it is confirmed.
to run
Even if you write a memo,
It is confirmed to be running.
Now, we want
that this function
should not be created
again and again
For that,
to optimize the function,
we use
useCallback Hook
Here as well
You get the dependency.
So,
Pick it up.
And put it inside.
That's it.
I hope you understood.
For the very first time,
a new function object
will be created and will run.
Then only will you understand it.
No matter how much I do,
Neither is your child component running
nor is it rendering.
And it is necessary.
So, I hope, guys,
We did a quick recap.
of our OZ
I agree that the video was long.
but it was equally important to explain.
Now you know how to use memo.
and optimize the value.
Now, this useCallbackHook
works like useMemo.
Sort of, they're the same.
You can understand it.
Here, if we look,
The useCallback hook is not a major component.
It is used
when you pass
a prop in a child component
basically a function
and because of that,
It is re-rendering over and over again.
To save that, you use the useCallback hook.
This name is a bit misleading.
But its function is the same.
Now, here,
useCallback Hook
when will it be re-rendered again,
a new function object
should not be created.
That's it.
The one that was already created
in your call stack
this handleSelect here.
If nothing has changed,
It should obtain the same memory address every time.
If you compare it,
It is the same memory address.
Again and again,
A new function object was being created.
To protect that,
We use the useCallback hook.
Whatever it is,
at least, useMemo, useCallback
and Memo,
You must have known from yesterday's video.
Finally, in React version 19,
compiler automatically
without writing it,
without taking it up
How will it be optimized?
How will it cache our application?
That will be interesting to watch.
So, guys, stay tuned, don't go anywhere.
See you tomorrow at 4 p.m.
Until then, guys, thank you so much.
Be careful.