How can React update the UI BEFORE the API response even finishes? 😳
In this React 19 tutorial, we build a real Follow/Unfollow feature using useOptimistic() and see exactly how optimistic UI works with the frontend, API, and backend data.
We first test what happens when you click Follow in a real application. Then we break down what is actually happening behind the scenes:
Click Follow → UI updates instantly ⚡ → API runs → Success = keep it / Failure = revert
In this video you'll understand:
⚡ What useOptimistic() actually does 🔄 Real State vs Optimistic State 👤 How Follow/Unfollow works in a full-stack application 🧠 useOptimistic() syntax and update function 📈 How follower count changes instantly 🔗 How updateOptimisticUser() receives its value 💾 How the API confirms the real state ↩️ What happens when the request fails ⚛️ How useState and useOptimistic work together
We are continuing our React 19 New Features series, so watching the previous videos on use(), Suspense, Actions and related React 19 concepts will help you get maximum value from this one.
The goal isn't just to memorize another React Hook — it's to understand how modern apps make actions such as Likes, Follow/Unfollow, comments, messages and cart updates feel instant.
Subscribe to Thapa Technical / Kodyfier for more React 19 tutorials. 🚀
⌛TIMELINE⏳ 00:00 Real-Life Experiment – Follow Button 01:15 What Actually Happens Behind the Scenes? 03:30 What is useOptimistic() in React 19? 04:02 useOptimistic() Syntax Explained 04:50 How a Follow Button Works in Full Stack 05:15 Hooks We Need to Import 05:30 Must-Watch Videos Before useOptimistic() 07:05 Creating the Real User State with useState() 09:30 Creating Optimistic Data for Instant UI 13:10 What Value Do We Pass When Follow is Clicked? 15:20 Understanding the Optimistic Update Function 17:20 Follow & Unfollow Updating Instantly 20:20 Final JSX Code 21:15 useOptimistic() – Final Recap
AI Generated·May contain errors
Video Summary
AI Generated
This video explains the `useOptimistic` hook in React 19, which allows developers to update the UI instantly before a server response is received. By creating an \"illusion\" of immediate success, applications can eliminate perceived lag during API calls, resulting in a snappier and more professional user experience similar to major social media platforms.
Hey, Gais, please take a close look at this card regarding user details.
The text written on this button says "follow"
and followers count right now what we have, we have 121
It's okay, so the moment I click the follow button, watch, I've already clicked it.
But after a few seconds, the text I had changed to the following.
It's true, this is your experience, but guys, if I were to
I'm now on Instagram, come to my story, you've arrived at my story, please take a close look
Like the button, oh my god, tell me one thing, like, I pressed the like button
Clicking the button instantly updates my UI to reflect the like button's new state.
Hey, I mean it's still taking time; tell me, are you liking this more?
Where you click, after some time your value gets updated, this one here
This feature is fun, where the instant UI is updating your value right away.
Tell me, is this liking you, is it just that it's possible?
with the help of use optimistic because this process, though it seems very simple to look at, is actually not
In reality, it's good to listen carefully. Look, when I'm following, just keep going, Phila, I'm here.
I'm following you, check it out—there's actually something like this in reality when I press the follow button
When I click, I'll get whoever is following this user
Incrementing by one will make it increase to two, right? Like I said, follow
Click the button, an API call would have been made to my backend, then that would be you
It's an API call; you can see it, it appears to you in this manner.
Look, how much there is here. First of all, this user
ID is, brother, which one is this that has just been pressed on the follow button?
Who was that user, I mean, who exactly was that user, like you, right?
technical I am, until now if you press my subscribe button
Press the button to go to YouTube and click the subscribe button like this
Alright, then, I'm technical, aren't I? So, first of all
It should be clear to my server, brother, that this
The subscribe button just got heated. Which channel's subscribe button got heated?
It's the same way with my case here when I came here
Click here to find out which user this was, so first of all, we need to
The user will have to find out with the help of ID once
We find out about it, then when the user has
There will also be some condition there when you click the follow button.
Brother, this follow button has already been clicked
He was following her, but now he wants to unfollow her.
You'll have to check it here too, won't you?
Bro, you have to calculate, right? Add one, don't you?
We need to subtract one, so once we realize this
who wants to follow, this unfollow, then we we our
We fire the actual update query, okay, just check this out.
and then finally, whatever has happened, we return it; ahead of us are our
front end, I say whenever like
When the button is clicked, our API must definitely succeed, I mean
That is the API we have for the account's new followers.
I will update it, and I will receive a success message in return.
Sorry, which one is this success message that I'm supposed to get?
You're seeing all this stuff happening in the background, so it has to happen.
Actually, it will just take a little time for the API.
It will make a call on the back-end, then check that code there.
Who the hell is that brother following, the one who's unfollowing him?
Here, you can see our value, which is on the base.
User ID, which is one, has the following one here.
But today's rate of patience among users is just that.
There, the user should click on Brother Fats, click on Fats
It is possible with the help of UI update.
Alright, and what does it do?
It gives you confidence, it gives you courage.
You understand, right? I mean, it's going to happen in the future, brother. It's just an illusion.
It hasn't happened even in Excel, but we know it will happen in the future.
Now show it right here, understood? I hope you've got it. It's fine here, so it's fine.
See that you understand the actual definition: what is a React hook? So you are given optimistically updated data, you.
Syntax, you can see here very carefully. If you look at us, this is yours.
This is very important because every time we update it or call it
What I mean is, inside the reducer function, basically now you'll see ahead — when we pass some parameters, if the value of this changes there
You must be updating this too, how about telling me now? Look, it's like this. You can see here, look at this, this is in the action, Biru Doodoo Reducer, and Biru, this is needed here, whatever it is, now give me this.
You were shown a two-second hole
I had applied it to Jānavuch.
Okay?
But now understand here how this will work.
Now I have the optimistic value here
You have to do it, okay?
So, take a close look
That is what you must do first.
You have a Use state
What we have been enduring since time immemorial
The second one finally your React version 19
We have a use for optimism.
I imported these two.
from my React
Now if you remember
So your React version 19
Its features
I'm uploading a video on it.
Okay?
So if you look here
So the new feature that was there
And this new hook of your React 19.2
Then we did manual loading of your suspense
then use API with your context
Then we've seen your use of the API with promises here, that's correct.
So the value we were getting on this scale
Come to this video
All of these videos
Its code had ended here.
Come to this place
Look here, come over to this place
Here
So basically, I used plus suspense.
Data for user ID 1 I
He wanted to catch it.
Yeah, you see, I'm refreshing the previous time, aren't you?
Until you see something on your skin
Until you receive the data
We show the loading animation
So I've got the user's details right here
Your work here is done.
Now we have this
if the user's data is obtained
So if anyone clicks the follow button
Then increment that count by one.
No, just decrement it backward.
You must be feeling it's just a simple usage state
It's not increment or decrement, Baba.
It's not that simple.
Alright, look at it from God's perspective
This is the data
This is the data used by the user's promise
And it was me who was showing up here.
Alright, now from here onwards, it's yours.
It starts
The journey of the optimistic hook
So I told you just like that
You will need to provide two data here
One of your real data
which is like in your back-end
must have been communicating
The second one is your optimistic data
Those we are putting on the UI
So that people can instantly
It appears as if the value is being updated.
So, this is your actual data
I mean, we're talking about this.
I need the data for user ID 1
All this data I got from speaking
It's inside it.
Alright, you can see it now.
I am retrieving data for user ID 1.
Alright, you've figured it out.
Is that it, isn't it?
So this is my actual data
So first of all, the real data that I will have
We need to put it inside the use state hook.
I mean, it has to be kept.
We have already used the state hook
This is your state variable, this is your updated function
And here you can see
We have used the object
Who is the fetch user there?
That is it.
So here I am with him
I have used the spread operator basically.
Due to which, I have these two extra properties here
These are our fields
Inside our data, within the table
When I call it
So, there is a name inside my users table
email is
password is
followers are
And this following is
I mean, this field is for the following
Right now, you have these two fields here
May be it's not with you
If you follow me
Because I have just recently
Caught up in the whirlwind of teaching
I have created this for you all
that I have added two more new fields which are
columns in my table of this user
has added
And I have set the default value to 121.
And I have set the second one to false.
I am assuming this
That you're seeing for the first time
You will see the follow button
which will have a default value of 121
So I've added these two things.
What thing do I need now
In my users table
followers and the value of this following
I need to update it
That's why we have dismantled it.
In this way, basically the spread operator
and the value of both of these
What have I put there?
Whoever has .followers in this
If the value of users.followers is
If you assume Badifault 121
In the same way, translate the following as well
Badfault will be either true or false value
So, okay.
Otherwise, we will consider BadFault as false.
So this is my actual data
I've kept the real data with me.
Succinct
Now finally you have to create your optimistic data
And it will be made in this manner for you
This is your syntax
Here you can see
This is the use of an optimistic hook
Inside it basically returns an array with two elements.
The first one is your optimistic user.
So, this is your data
What we are going to display on the screen
and by the way, for the very first time
The data you have is only for your current user.
Alright
And one thing to remember
This is important.
Because I told you so
Whenever its value changes
So use an optimistic hook
Inside, we pass two arguments
The one who comes to you first
That is your actual data
Your actual real data
The ones who were there earlier, I received help from the state.
If you look
We have a function
You could also call this an optimistic update function.
It's basically a fat arrow function.
You can see here
So I'm passing two parameters inside it.
The one in which my very first data is
It's my user object
It's a fake copy.
Here, I was the one who had gotten him here.
The current user is nothing else.
Rather, the real estate data I have
That is it
My actual current user, which is this user's data
Philal is here.
then what you are seeing here in the second one
This is important because
The current user is, by default, this one
But this second parameter here
Where will I get its value?
Yeah, and when am I calling you?
So guys, the value of this is yours
Meets this world
Here, my heart says yes
I told you just a little while ago when we were watching, didn't I?
Its syntax, this second one regarding syntax
The argument is here.
The second element is its value
If changed, then the value
Yeah, we're passing this on to the following.
True or false
Now you will know what comes next
You can see here again this current
We saw him here.
has spread it so that
Now inside it, me
First of all, it will become clear then
And the second one is yours
followers are there, then this one
Two data points are available; these are your optimistic ones.
the value that I currently have in Excel
I am about to put it in the UI.
It's simple, just check in followers
current user's followers
What does it mean?
You are mistaken.
Suppose Abi, your current followers
120-121
Now someone has clicked the follow button.
So, if the value of the following is true
Here you can see
The value that is this one
plus 1 will become
If the value of the following is false
So, this here is yours
It will become minus 1.
I mean, if someone came here and
Take it when you come here
It seemed like this way of following.
And when you click
What do you mean by you to him
If you're following, add 1.
And there, if this is your on follow
What would you do?
Brother, I don't need to follow this.
This is your optimism
Alright, so in this way we do it
Writing, I understand.
Look at your real data, user
Here too, I am setting two values.
And the second one is my optimistic data
This is two, and by the way
You must be feeling a bit doubtful about this.
So basically, this is something like that for you
If both key and value are the same
Just typing the key will also work.
Here, the value of this following will be true.
And from where am I receiving it?
From here to here
Alright, now this value here, this is what I need
When will it be available, and when will it be updated?
This is important; you will see from the goat
You've already figured out that optimistic
I'm just changing the two values.
Because of this case of mine
I am interacting with this button right here.
Who follows me and my followers
It's changing; nothing else is being done anywhere else.
That's why I've placed these two in this manner.
Now come, Siddha, to this place
When I press this button
I'm calling the API by clicking.
So this function here, it's mine
The button needs to be checked now, first of all
Real data is ready with us, optimistic
We have the data now we
Come here where that button is
Oh my god, here we have this button
And all of this is one and the same continuous thing.
Our user profile that we saw yesterday
Look inside it right there
optimisticuser.name
What did I say, then?
Remember this rule: when you are optimistic
If you use it, it will be on your UI
All the data that exists is yours, optimistic.
should be the case, meaning ripped from the start
Look here, look here, look at this place
The way you use the state hook
If you are passing through it, whatever you pass inside it
Where is that data stored?
The user has it too, in the same way, use optimistic
Whatever you passed in the hook as well
Where did that optimistic user go?
and by default you are here as user
the optimistic user has our current user
The data has arrived.
that's why
That was the user's actual data, which was previously in a relaxed state.
field named name email
Followers, that's right here, we have it
This is the button that will pass it.
It is important to look at Gorch.
Here we are using a form
You can see it
Until you see the text
He played then follow
Then they played, that was this tifr-firfiše
If the following is true
Then it will show you the following
If this key's value is false
Then it will showINCLUDED
The issue now is where the value of this following is still being obtained from.
How can it be determined whether it is true or false?
So finally, when you click this button, we are calling a function.
Follow action, all your doubts here will be completely cleared.
So first, look at this, the very first step
Whatever value user.isfollowing has, I'm passing it along.
Assigning now, you're looking from here, okay?
What does user.isfollowing mean, that is, what is the meaning of user?
If the value of the following is true
What exactly happened was that you followed here in this manner earlier
There was a button; I clicked it, and now what has happened?
that I am following this, meaning my following of this
What is the value? True means true, and the meaning of true is that I understand this
Following you, okay, now back to him
If the user clicks this button after following, then
That simply means he had already given up on following.
If he clicks back now, it means he wants to unfollow.
Don't click, meaning when he clicks for the second time on this place
So then what will happen, it has already been called back, hasn't it, this wake-up?
Pay, this call has been made. As soon as this call was made, user dot
What is my value right now? Is it true?
No, if you don't say it, it's true, but what am I doing here?
What is the NOT operator of true using कराँ?
I mean, this time, your next following state, which is
The value has turned to false, and here is your Mega Superstar update
What is the next state for the optimistic user? Is this false, and its
if the value becomes false, then that is our second parameter
What exists has its value; this is it, this is the real thing.
As soon as this update happened, it's as if this update just happened, isn't it?
basically, whatever value you have entered here
It turns out that this function of mine is the following.
It's gone there, where my second parameter is.
False. Okay, now this is false, so this is your false.
If this is false, check currentuser.followers then
followers when I clicked, when I unfollowed and then followed back
121 Her followers became 122 But I came back from
Clicked it, so what is the value of this following now? false
Look closely: what is the value of the following at 122 plus here?
My current status is false, false, I mean this part has gone
The ternary operator, the last part came out as 122 here
It's minus one, isn't it? Plus minus, so obviously it will be minus.
and you got 121, that's it. So, guys, in this way, your
It's working, and this is your optimistic value because
It will show either 121 or 122 from the crack.
Assuming that the API will eventually succeed, since
It must increment by one each time you click the "Follow" button.
Following someone means it goes up by one, so unfollowing means it goes down by one. That's it.
Your optimistic value is the optimistic user's followers
As soon as this happens, your value gets updated. Just look at this, I mean the API.
A call is coming through later, but regarding your UI
It's updated, as soon as this line appears, whatever you have here
I have followers, I just did something reckless somewhere in a second.
Look closely at your value right now
Just click here, right here
As soon as I clicked, this function was called, and this is the value I received.
Get it and update optimistic, I've already called as soon as this call
My UI has been updated, and the funny part is, check this out—this API
We haven't even made the call yet, let alone the API call after that.
He has gone there, meaning it's fine too; the user has already clicked, the user who
That ID was there, I'll pass it to this API when I call it.
The method because I need to update the followers' account
We must clearly indicate inside the headers, and inside the body, I have simply done the following:
The data is there; I just passed true or false, and that's it, we're done.
Is the code for our server written here in this way true or false?
Hey, check this out— if it's true, update it from one.
If true, add one; if not, subtract one, and from that result
What I have is my new follower's account updated, or in other words, plus one.
I'm doing minus one, so this is it, guys, your use optimistic hook
And if everything is not right, follow update fail with this method
You'll see, won't you? If everything goes right, then you'll see.
The data has been obtained. Updated user, and the data—where did you upload it?
set user means adding an entry in the set user, which refers to your actual real data
He went to the user, and when he went to the user, it means yours.
The optimistic user received the data, got it, guys, follow this
Hey, look here, this path is going this way—see?
Khattam, and once this happens, the rule is simply that every time
You need to write this: optimisticuser.name.email.followers, and do it this way
the data of your website, the content that exists
You can do it super fast with just one click, so guys, you've got
You must have seen the UI before the API, brother—that's exactly what it means.
Whether he clicked follow or unfollow on it, I don't know.
It will be seen in the matter, but the user understood just fine—Bikor followed him.
Clicked unfollow because that condition is here, plus one or minus one
You will really like the video, you'll definitely like it, you'll definitely like it, you'll realize it
What role does this optimistic approach play, and in what way does it function?
Works, and whatever you see on large websites instantly
What is the reason behind it? You've also figured out the optimistic values.
They are using it, and later, when the subsequent API call occurs
If it succeeds, there's no tension at all; if it doesn't, it will come back and show you just how much you've failed.
Got it, okay, and if you like today's video, the rest of the pre-base videos
You must definitely check this out because it's based on the Latice feature of version 19, which I have