- Podcasts
- ReactJS Developers Podcast
- React State Management Explained – Master the useState Hook in React.js
React State Management Explained – Master the useState Hook in React.js
ReactJS Developers Podcast
- December 3, 2026
- 1 h 16 min
- Episode 3
About this episodeIn this episode, we explore State in React.js and the useState Hook, one of the most fundamental concepts every React developer needs to…
- What state means in React.js.
- Understanding the importance of state management.
- Introduction to React Hooks.
- What the useState Hook does.
- How to declare state variables.
- Understanding the state setter function.
- Updating state in functional components.
- Handling user interactions with state.
- Managing multiple state variables.
- Understanding React component re-rendering.
- Common mistakes when updating state.
- Practical examples of the useState Hook.
- Beginners learning React.js.
- JavaScript and frontend developers.
- Software engineering students.
- Developers transitioning to modern React.
- Anyone interested in building interactive web applications.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
React State Management Explained – Master the useState Hook in React.js
This, that means you have covered almost everything in react. As we understood the state as props, we will also understand the state in depth and it is also very important. And I will tell you right now, please go in the sequence. Step by step, go first, second, third. Now you can understand what I am trying to say. There are all videos in the sequence. Before this, we have discussed props, components, JSX, etc. And in that too, I told you a lot of things, I gave you an idea. So in the sequence, it will be a lot of fun. Crystal clear understanding. Okay, so state, heart of react apps, definitely we say this. To understand the state, I will give you an idea of what I have told you. I have told you the components so far. We can style some of our components. The circle we have made is our component. Now I am repeating again and again that there can be some data near the component.
Wherever props and state are coming, that means data is working. Data itself is very important. And props and state are both related to data. I have told the props, so I will not talk about the props in this video. I can do a little, but I will not do much. There is a lot of discussion on state. Props was our external data. Definitely, we were making the component dynamic. If we want to send external data in the component, if we replicate the same component again and again, then the same UI, same data, then there is no meaning.
Wherever I am talking about data, you have to understand that I am talking about state. And wherever I am talking about state, you have to understand that I am talking about data. If this much is clear to you, then definitely the lecture will go smoothly. State means data, data means state. The data of the component is the state of the component. Keep this in mind, the data of the component.
What we do in React, I had told you earlier, React has one-way data binding. That means, although there are many things, for now you just understand that here we are playing with data. Whatever we do, we do it with data. Normal HTML, normal HTML, plus CSS, plus Java script, if you have read it, then you must have seen that here we do something like DOM manipulation.
Where we are definitely playing with data, but if there is a need to make some changes in the UI, this is our UI. If we need to make changes in the UI, then we do DOM manipulation here, select the element, and then select it and change the text inside it, innerHTML, etc. That is called DOM manipulation. Here we are not doing anything like this in react. What we do in react is that we change only data here.
So you have to keep this in mind. I have just told you 2-3 things. Wherever I say data, you have to understand it. State, state, you have to understand data. Data is state, brother, we will go together. And by changing it in the data, your UI gets updated. We have seen this before. I have given you one more recap. We will change it in the data, the UI will get updated. There is nothing like DOM manipulation here. We are not going to do DOM manipulation here.
First I select it, whatever add event listener has to be applied, first I select it definitely, document.querySelector selects it, then after that, whatever event listener is applied on this button, then I change its inner text, something works like this. But here I don't need to do anything like this.
So here as soon as I click on add, I have nothing to do with this. I have nothing to do with what is happening in the UI. I will simply change the state here. State means I will try to change the data here. As soon as it clicks on add, we will definitely handle the event here. We will try to change the state by handling the event here. Nothing in this. We will not go to this side. We don't have to go to this side. We have to go to this side. We will make changes in the state and it will automatically reflect in this direction here. Listen carefully what I said, this is very important, I will focus on it in the starting, it will grow very fast in the future. Listen carefully again, I said this is our button, we will definitely handle the event by clicking this button. We are going to do event handling here, there is no doubt about it, we will do event handling. But while doing event handling, we will not try to make any changes in it. We will go to the state here. Our state will automatically reflect here. We just have to make changes in the data and I have written that here. Change in state directly leads to a change in the UI. You will change it in the state, it will be changed directly in the UI. And this is basic, we have already done it, we have also made projects, so this much is clear to you. Let's go a little further in depth. All these things will also be clear to you, if not, then I will revise it a little, I just told you now.
This is big, this is big, this is what you see, this is your component. Props are external data. If we want to send something externally, we use props. It is read-only. We cannot change it. Read-only means we cannot change it. If we talk about the state, then the state is the internal data of any component.
But why does state need data? We can make it anyway. We can make variables anyway. We can make arrays anyway. We can make objects anyway. Then why is state needed? Let's understand this. We will try to understand this quickly. We will definitely have to come to the code. So this is our code. This is our code. I have installed this bit project. You can do all this quickly. And I will remove it from here. You must be seeing our code now.
So nothing nothing nothing much nahi ho raha hai alright abhi main yaha pe console karta ho CLG COUNT abh dekhte hain deload karte hain main ye increment oh my god oh my god yaha pe to increment ho raha hai yehap pe to increment ho raha hai yaha pe to increment ho raha hai yaha pe jaisa hi main increment kar raha ho console mein to aapke increment ho raha hai that means that means Your actual variable is being incremented. So here it is being incremented. Here I have controlled the variable. That means your actual variable is being incremented. But the update is not happening on the UI. And just a little while ago, I told you that the state is such a thing where we just focus on the data. We change the data. It automatically reflects on the UI. This is what we needed in React. This is what we needed and we are not getting it here. The data is changing. We have created the variable. Data is changing, we can see it but not updating in UI. Then we will have to do DOM manipulation. Then why are we doing it? React is getting insulted. Come on, let's learn state quickly. Now you understand why state? Why do we need state for dynamic UI? We have created dynamic UI. See, as soon as we changed it in the data, there is no reflect on the UI. We can say that this is static UI and it is not working dynamically. If we talk about the counter button, which I just gave an example of, then without state, I have clicked on UI and want update. If I am clicking on the click button, I have written the logic and the update is also happening in the variable, but the UI is not changing. If we work with the help of the state, then your update will be in the UI only by changing it in the data. Only by changing it in the data, the UI will be updated. So let's come here and understand a little about the use of the state hook. The hook was being discussed for a long time, I was not discussing it. Many people say in the comments, where is the hook? So this is our hook. There is also a hook in the use state. We will discuss more on the hook.
So for this we use useState in which s capital is written in the camel case. useState is a function like this and here we put our initial state. What were we doing in the variable? Normal, late, count is equal to zero. So whatever is written here, we put it here. This is our state, data. We will make the data like this. So what happened by making it like this?
Listen carefully, I will understand everything. We will use useState, it will return you an array. Now there will be two things in this array that we will destructure. You know what destructure is, you have read it in JavaScript, if you have not done it, then there is no problem, you will understand this much. So it will return an array. Now we will get two things in this array. First of all, we will get a variable which we were making a count variable. Let's name it count. For example, let's write count here. We got the variable. What will we get extra here? We will get an extra setter function here. We will get a setter function here. Understand carefully what I am telling you now. I am going to tell you a lot of things and I am going to tell you a lot of things in this lecture.
In this setter function, we have given its name as setCount. You can give any name of the setter function. In the array, you are getting two things. One is the variable and the other is the setter function. The name of the variable is A2, B2, Alpha2, Beta2, Gamma2, Count. In the setter function, you have setA, setB. Is it necessary to set further? Even if you don't set, it will work. If you set further, it will know that this is the setter function which.
So, we were making this variable earlier also. What did we get extra? We got this extra which is very important. What did we get extra? We got this extra second function. And this is very important. We are going to understand this now. On this, we are going to talk about everything. There are many things like functional update in this which we will understand. This is the function by which we will change this variable, we will change the data and the UI will be automatically affected. We don't have to do anything. We just have to change the data and use the setter function. We will not update directly. Let's see how you will do it. Let's see if there is anything else written in the notes. So it is written here that syntax and destructuring. So let me explain you a little bit about the concept of destructuring here. I will explain you the concept of destructuring in the browser itself.
So here I made an array from the left. Array made here is 1, 2, 3. Now this is your array. What is the way to access the array? Suppose I want the value on the first one. If I write array of 0 here, then the first one will come. If I write array of 1, then the second one will come. If I write array of 2, then our second index, which is 3, will come. Something like this happens. This is what I want to destructure in a variable. Like we have an array here. Let's do it this way. Let me tell you that there is nothing much to destructure. You have to make an array like this.
The first index has an alpha variable and the first index has a minus, then the alpha variable will have a minus. And here our second, first, sorry, this was the zeroth index. The minus will come in the zeroth index and the zero will come in the beta in the first index. Okay, you know this is the concept of destructuring. If I want to see alpha now, then inside alpha, Alpha is not defined. I have written the spelling wrongly. I have written APH. So, there is a minus in alpha and.
So I know that the error will be returned, I will use the useState, I initialize it from 0 here, then I will get an error. So let's console and see the error quickly. And there will also be an error, because we have used a little count here. We have used the whole count, there is no count here, so there is an error. So don't even focus on the error, for now, go here. This is the array that you have. In the array, the state is on the 0th index. The data that we have stored in the variable is that. And there is a function on the first index. Can you see? So I will destructure this. What is coming in the first place? The variable is coming in the first place, which I will name as count. The function is coming in the second place, which I will name as set count. So the first one is a variable and the second one is a function. Now you can see it if you want. So let me see what is here in count. So in count, definitely you have here. zero aara hai, do baari repeat ho ra hai, to mujhe thoda sa, say, a little allergy. I don't want to see it twice. I have used it once, so I will see it once. I will remove this strict mode. So here, see, let's reload. So here, see, the count has come to you.
And if we want to see the set count, then in the set count, definitely, what will happen to you? In the set count, you have a function here. Okay, so function definitely, I will use it and show you now. So very good, so I have used this count here. Now what am I going to do? Now I make the function separately, here we do not do confusion, garbage, otherwise you will not understand anything. I can make it on click here too, there is no problem, but from the point of view of explaining, increment. And I make this increment function here. So I made an increment in this function and used it here. Now what will I do? Now listen carefully to what I say. Now I have used const here, so I can't change this variable directly. You will say, let it be. Let me show you. I have let it be. And here I change count to count plus one.
All right. This is our count variable. Count is equal to count plus one. These are the basic things of children. I have told you this. Let's do CLG count. And let's see if our UI reflects now. Because you said that if we use useState. If we use useState, then when it changes to state, it also reflects on the UI. So what will our reflect be? Think a little and tell me. What will our reflect be? Come on, let's reload. Money key increment. Again, here it is reflecting but not in the UI. Then what is the state? Remember that day, just a little while ago, I just told you. Where did I tell you? Did I make it rough? You are getting a variable in the use state but you also get an extra z function.
Definitely, so that we can update this variable through this function. So if we update this variable through this function, then our UI will change. Now why do we have to do this? I have explained it in detail in the previous video. I have taken slides and all. So don't get too confused now. Let's just understand. So now instead of updating like this, instead of updating like this, now I will use setCount here. And here I will do count plus 1. Now understand this setCount is our function which keeps the power to update this count. So if I give 5 in setCount, then as soon as I click on increment, the setCount will set 5 in the count variable which is our state. The setter function has only one function. Set it and after setting it, render the UI again. Don't go too far, I will explain it in detail later. It renders again so that the UI is updated. So the function of setter function is the same that it will change dynamically as soon as it changes in the count variable and after changing it, it will be updated in the UI. So now come here and see what will happen as soon as I click on it. If I click again So definitely again it is doing 5, but if it is 5 of 5, then it is not known. So in this way we work here. We will always use the setter function to update here. Keep this in mind. And there are a lot of depth things in this, which are still to be discussed. Now the basics are coming, so don't think that the basics are over now. So now what will I do here? I will use the old state which is our count and I will not put a plus in it. So as soon as the first increment calls, what is the old value of the count? 0. So what will be 0 plus 1?
We just made a state here. We got a function in this state. We updated it using that function. Here the UI is updated automatically. Okay? If you are clear on these few things, it is very good. Here we just got a setter function extra. You might be thinking that why should we use such a big syntax.
So you must have understood so much. I will read a little for you. And for me too. My revision will be done. So, here in syntax restructuring, first of all, you get this count which is a state variable and second is the setter function. So, what is a state variable? A state variable is a special variable. This is a special variable which is a normal variable. You were not getting this functionality because it is a special variable. What is extra in a special variable? A special variable in React that holds data that changes over time and should trigger a re-render of the component when it changes.
React to update the state and re-render the component. The work of setter function is to update the state and re-render the component. Ensures changes are tracked by the internal system of React. The setter function is made through the internal system of React. So that it can be tracked and reflected in the UI as soon as it changes. I will explain you in more detail. Just keep a little subreddit. And what did I say? I said don't modify directly here.
Never modify state directly like count is equal to count plus one you have done directly this is not done directly mutating the variable won't trigger rerender this doesn't trigger rerender and now I have a question from you the question is that the question is that we mostly use const here although we can write let too it is not a problem but maximum time everywhere you will see const const state comma setState is equal to useState. Why do we use cost here? It is a very simple answer. I will tell you quickly. To avoid accidental changes. To avoid accidental changes. What happens with beginners is that instead of doing setState, they start changing in the state itself. which happens, but it does not reflect in the UI. So you have the idea of the post keyword, you have read it in JavaScript. Whenever you try to change the constant variable, it gives you an error. So as soon as it gives an error, you will know that you do not have to change it directly, you have to change it from the set state. This is a simple reason, there is not much. Okay, that's all you understand.
In state also definitely. In state also we can make number, we can make string, we can make boolean. You know boolean true or false. Then after that, we can definitely use object and array. Should I show you? Come on, I'll show you. Although I won't be very angry, otherwise you will get confused. So let, let, I make a user data here. User data comma set user data. Then use state. I can also make an object here in this way. There is definitely a key value pair in the object.
I will tell you all that in the next video. In changes, I will tell you what to keep in mind while changing the object. You have to use the setter function. But there are some other things that we have to keep in mind while setting or updating. We will see the update properly now. It will be clear. So you will be clear so far. We haven't done much yet.
The meaning of state is that if we change in state, then it should be reflected in the UI. Normal variables cannot do all these things, normal variables cannot do this. So we made a state. The specialty of the state is that it gives an extra setter function. So by using the setter function, as soon as we update it.
Powerade components are rendered and updated. Here I will explain to you how things are done very well. And before that, I will tell you how to update things. After that, In the previous component lecture, I have told you about the concept of memory. I have taken this slide because there are some extra things that I will tell you. After that, we have keyed components. You must have seen that we use key while mapping, so today you will know why we use it.
But there is one more way here. This is your second way. Now where is it actually used? When do we use it and what are the benefits of it? The common pitfalls that I have just shown you. There we will discuss what are the limitations. What are the things that react has done for performance improvement?
We will save it in the card component we will use the card component directly here that's it we have shown it here, we will reload it we will increment it, increment is done, wow so what we were going to tell you, we will update one more way of updating the state So, look carefully here. This was one way to update.
Okay. By default, it provides you with a functional update. You can understand it as a higher order function. You can also treat it as a higher order function. You did setCount. You don't want to use the name of this variable here. The reason for this is that that they use for functional update, batching etc. That is a different concept. But now I want to explain to you how to use it. So here you make a normal function, whether you make this function or you make this function.
In this function, by default, you are getting a parameter. By default, you are getting by default, what you are getting is the value of the state. You can give any name here. Here you give a useless name. What value will be in this useless? The value that will be in the count, in the previous count, that is, the current value that will be in the count, you will be getting that which is zero.
I will show you by console. I am not doing anything here. I am just doing console here. Set will be done when I return. I am incrementing here. I am not doing anything here. That's why it is empty. Here zero is coming. That means what is coming in this useless? The value of count is coming. You just have to keep this in mind. What did I say again? I said that we can do this.
We can give any name here. Suppose I gave a useless name here. Most of the time we give the previous name. That is, the previous value is getting here. And here we will return previous plus one. So the same thing happened. The work we did here in one line, we did here in three lines. What will be the benefit? There is definitely a lot of benefit. We use it a lot. I will tell you later. Now just focus on this syntax. It is also working here like the higher order function. Let's make a function in it. Here we were getting the previous value. In the previous value, we added one and returned it.
So I will do plus 1 in the previous value and you will have to return if you are applying curly braces, then you will have to return. So if you have not returned, then it is coming and if you do not want to return, then remove these curly braces. Because it is a one line code, then these are all part of javascript. And here because it is a single parameter, you can remove this too. So this is your way of writing sort.
So here it is written in this way. So you understood syntax wise. What is it used for? I will tell you later. I want to tell you properly, that's why I didn't tell you yet. I didn't confuse you. Now understand this line a little bit. I want you to use your brain for this line. I am saying this because many people have not read JavaScript, so they are confused. Those who have read JavaScript must have understood these two lines by now.
To update something like an array object, then do not directly copy it. I used to say this in JavaScript. Here is the same font. Immutability is the concept of use. What is immutability? Don't change. You have read all this. Mutable means change. So don't change directly. Like you used the useState here. And here you made an object. So definitely now I am telling you that if you change directly, then there will be no re-render.
Here so whatever state you have made const here you have an object here set object like this so if you directly push in object or dot and put data like this then definitely update will not happen you have to use setter function and in setter function also you have to keep in mind that you don't change in variable Yeah. Yeah. If you directly change it, then what will be the benefit? Directly change means you can push object. There is an element in it. You can push any object here.
The name of object.key and try to change the object.name directly. So don't do all this. Even if it is const, it will change. You have read all this in javascript. Even if it is const, you can directly mutate the object. Because object is mutable. But you don't have to do this because reflection will not be possible in your UI. use the setter function while using the setter function, you have to keep in mind that there is no change in the original object. For this, you have to make a copy of the object and work. How? So this will be our set object. In this, definitely whatever we put, it will be set in your object. Or you can make another function in the setter function and do something.
And this new object will be made a copy and the original object will be changed to copy and will return and will be saved in this object. The second way you have is the normal way that you make an object directly in this way. See what to change. For example, I have made a dot dot dot object here, so this old object has come. This is a copy of the object, this is a full object, it has become a copy. So here you have made a dot dot dot object, whatever changes you have to make, you have to add, or if you want to change something in the name, then you can do it here, you can add something.
No change in the original object, through the setter function, you can go to this object and set it back. So you have to keep this in mind, you don't have to mutate directly, okay? If you don't understand this, then man, So common mistakes are that we directly mutate it. Never modify state directly. This is an old example. Directly mutating the variable. So let's understand this a little bit. And yet components are rendered and updated. This is a little theoretical part. But it is important from the point of view of the interview. First of all, state declaration work is understood. How do you declare state? So components in react manages their data using state definitely. Now every kid knows that in functions components, this is typically done using the useState hook.
When the state is updated using its setter function, when you update using its setter function, React detects this change. Internally, the algorithm of React detects this change. They have made setter function for this purpose. It detects this change. It then queues a re-render of the component. You know that queue is the job queue in a queue. First in first out concept, a little bit of data structure concept here. Suppose you have a task which is placed in a line like this. You get a task in the same line and it leaves it to schedule. It then queues a rerender of the component where the state was updated. Wherever the state has been updated. It may be that the state has been used in the child component as well. It may be that the state has been used in the child component as well. So wherever you have used the state, if it changes in that state, it will rerender trigger. It will schedule to rerender. Why am I talking about the schedule? All these are a bit complex things that I will tell you properly in the future.
Whenever you use the setter function, it detects the change and goes for re-render. All the components are re-rendered in the child component. You know the concept of virtual DOM. During the re-render, React re-executes the component's render logic. Whatever is the logic of React, it is re-executed. It compares the new virtual DOM with the old one. It compares it with the actual DOM. It makes a new DOM. For example, the old DOM is like this. First of all, it will create a virtual DOM like a light bit copy. If something has changed in this, then it will be a virtual DOM. This is your virtual DOM and this is your real DOM. If there is a comparison between virtual DOM and real DOM, then it will only change the necessary part. It will not change everything else. This makes the performance better. Previous one efficiently updates only the necessary part of the actual DOM to reflect the new state. This ensures that the UI accurately displays the current state. So after changing, the current state, which you have changed, the value of that state starts to appear. So this is how it works here. You changed it using the setter function. According to the internal algorithm, it detects. Then the component surrender logic runs again. The virtual DOM, the real DOM, it does whatever needs to be changed there. That means definitely the data that has been changed and the data that is being used, by changing it there, your new state is reflected. This was also a little above, we are going to go a little further in depth. Now if we talk.
If I tell you here, if I tell you here in app.jsx, this card, I will replicate it. I will replicate it and show you here. So, look carefully. If the memory was the same, then I would have incremented in one, I would have incremented in all. But this does not happen. The one I increment, the value of that component is incremented. That means, in every component instance, I am making an instance, right? I made one component here. I have created a component. I am using that component multiple times. That means I am creating its instance. So every instance has its own state, its own memory. That's why I am incrementing. So every state has its own increment. Can you understand?
I have already told this in a lecture. So let's see it quickly. Each component Each component instance has its own independent state, even if it's the same component used multiple times. Same component multiple times you are using, still you have independent state memory for every instance. Why it works like this? How does it work like this? So, React does not attach state to the component function itself. That means, the component you are making does not attach the state to that component. From the instance of the component, where you are using it, it makes different states separately from the instance and attaches them to it.
So, the state does not attach to the card. It is not that the card will be attached to the state and the card will work in the same state wherever you are using it. Whenever you use a card, a new instance is created here. Make an instance. The state of this instance will be made separately. The state bucket will be made here and it will be.
Then its instance will be made and then its state will be made according to its instance and it will be linked with it. Then you will make, then a new instance will be made and its state will be linked with it. So this is how your work is going on here. And let's move ahead. Here you have, React does not attach state to the component function itself. Instead, React keeps a state bucket for each component instance. It makes a bucket for each component instance in tree, something like this. When React re-renders, it matches components using their position. According to their position in the tree, it matches which is the component instance, which is the state, and whatever data is there, the memory is there, it gives it back, where you are using it, it works in this way, what is the use of the key, you must have seen that whenever you map, it is important to give you the key, it is necessary, it is compulsory, why is it like I was using the card component in this way, now I want to make an array here and use it, 1, 2.
I will do array.map. Here, this is also a higher order function. It will take a function like this. And here, by default, you have items coming. One item, 1, 2, 3, 4, and it will be coming like this. I will return card here. So here, the card will be rendered 5 times. 5 times your card has been rendered. And here it is giving me an error. Each child in a list should have a unique key prop as a key. Why should there be a unique key prop? The unique key should be there. The unique key should be there. React uses the key prop. When you send the key, the key decides which state is attached to which component. Decide which state belongs to which component. Components are being rendered multiple times. One time it is getting carded, second time it is getting carded, third time it is getting carded. Now which card is this, which state is this, which memory is this, how do we know? We know it through this key.
Saying exactly that if you do it now, then you will have the same component, the same state will be linked to you, you will change it in this, it will change in that. This does not happen. But in future, there is a possibility of a bug coming. It is possible that React will fix it later. But for now, you should give the key. You should give the key. If you don't give it, then it will work. Now see, as I have not given the key. Now I have not given the key here. So if I change its data, it is the same, its data changes, it is the same, its data changes, it is the same. And here it is saying that each child in a list should have a unique key prop. If I give it a unique key prop here.
So everyone has a unique key. Now if the unique key is done, then I should not get an error here. This will work properly. You get a good performance from it. So you should do this. And suppose I take the same key of 2 here. This one is also 1 and this one is also 1. So here it will give me an error. Encounter two children with the same key 1. Two children have the same key which is 1. Keys should be unique so that components maintain their identity. How will it be known that they are different components? How will the state linking be there?
So you have to give the key props properly and try to give a unique key prop. For that, you can also take the index here, the index will come to you differently every time and you can give the index here if you do not get any unique ID from here. I hope you understood this too, till now you were reading that what is to be given here, what is to be given, why to be given, I told you all this and in reacting to this, keep on deciding which state belongs to which component.
But if you are asked all these questions, then you can easily answer them. Okay? Did you understand this much? And if you understood this much, that's it, then comment below. Let's take a break for a while. I am talking for a long time, so I am also tired. I am not drinking water, I have already done back and forth. Isn't it?
I told you about the immutability concept. So just recap this once. See if you will get notes in the description. If not, then comment. I will give it in some time. As soon as I remember. Sometimes I forget. So as soon as I see your comment that notes, notes, notes, notes are coming. So then I remember that I forgot to give.
It is fun to study like this. This will be a continuation for you. Okay, now we are discussing some common pitfalls. You must have understood the pitfalls. Here you can say some hidden mistakes. Mistakes should not be called exactly mistakes. Because for some performance improvement, react has done all the things that I am going to tell you now. But most of the beginners get stuck in this. Expected results are not coming. That's why we call it a common pitfall that there are some mistakes. basically this should not happen. If we see in actual, all these things are done for performance improvement, which we think are some mistakes. I will tell you now, you will understand, otherwise we will be stuck here. So common pitfalls, first of all, the async nature of state updates. Now don't get into the exact asynchronous programming. Async nature means that the state does not change immediately, if we update in state then it will not effect immediately, it will not impact on the UI immediately, react schedule it and re render later, after some time it will re render here, means I am saying that I am in the card component, I will close it. Here we have seen all these things.
If it is 0, then it should be 1 and in the count we should see console.log 1 here. We should see 1 in the starting and also 1 in the UI. So reload, now 0 is visible. As soon as I increment, I should see 1 in the console. It should be visible, yes or no. I did set count here, so count should be 1. And we should see 1 in the console. Updated value should be visible.
I can see 1 here but I can see the old value here. I can see 0 here. If I click again, I can see 2 here and 2 here. I can see 2 here but I can see 1 here. That means it is giving me the old value. Why is this happening? You will have to understand this. Here it is following a little async nature. that whatever event we are handling here, like our increment event, so we have made the increment function here, then the entire synchronous code will run first, then the state will be updated here and the re render process will happen here. The state will definitely be updated, but the whole synchronous code will run here and then the re render will be scheduled here. And how it will be scheduled, all that is decided by react. So in this way your work happens here, there is no immediate effect. In Async, the same thing used to happen, the whole synchronous code used to run, then your Async code used to run. Here all the synchronous lines will run for you, then your Async code will work as it used to.
OK. And here re-render is scheduled so that the re-render process takes place later. So first we see the old value, which is zero in the count, that will be seen and then set count will be re-rendered. Then when we click again, then we see the new updated value here. This is how we are working here. Updating state does not happen immediately. React schedules it and re-renders it later.
Here we can see the old value. What is its solution? I will tell you the solution. The solution is the same functional update. We will use a function in set count. We will use the better function. Here we will get the previous count value. and you can see that previous count here previous count and here if you do previous count plus one in previous count previous count plus one then here you should see let's come here and reload I have done increment here something went wrong I am so sorry, we have to return here as well.
previousCount, whatever count you can give here, there is no problem. So the previousCount we got here, which is 0, we have incremented 0 from 1, and the new updated value we have consolidated here and then returned the previous value, so the previous value will return here, it will also be updated, along with that we will also be able to work on it, the new updated value we have. As soon as we increment, 1 is visible here, 1 is visible here, then we increment, 2 is visible here, 2 is visible here, so this was our functional update.
And here I am reloading and as soon as I increment it will show updated value. So you don't have to go on this, you won't understand it. Here I was trying to explain that you can use functional update. It works on current value. If you do console inside it, then you will see current value. If you do outside, then again it is not visible now. And there was a use effect method. I will tell you many more things now, then you will be more clear. This was the async nature of state updates. After that comes the batching of updates. Batching of updates means, let's say I have made a comment here and here I am doing set count, count plus one. then count plus one, count plus one, count plus one, I did count plus one four times. So four should be added in zero, first one will be added, so here your expected is that one will be added in zero, then if one will be added in one, then two will be added, then if one will be added in two, then three will be added, then if one will be added in three, then four will be added. So on UI, we should see an increment of four in a single click. What did I say? I did count plus one, count plus one, count plus one, count plus one. We should have seen 4 increments in the rule. So, if there will be 4 increments in 0, then it should have been 4. So, do we see 4 here?
This happens because react uses batching concept for better performance. If you are updating the state again and again and doing it at the same place. This is your only event, increment event. This is only one event. In one event, you are counting set four times. If you are counting set four times, then instead of updating it again and again, listen carefully to me, instead of updating it again and again, it converts this whole into a batch. And when this whole event is completed, then it performs that batch and your work is done here only once, which is this one. And if it works only once then it will be 0 and 1. I have written here react-batches-multiple-state-updates. Do you understand batch? Combine in group. It combines all three. React-batches-multiple-state-updates in the same event. If it is same event then it will batch to improve performance. Avoid unnecessary render. Performance wise, you will think that you should not have done this, you have to update multiple times. So think for yourself, if you are doing it again and again, then you should see the last update here.
The last one is rendering only once, even if you did it four times, but it is rendering only once. Which is 0 plus 1, your answer is 1. So here count will increase only by 103 because react batches them and each line uses old count. And I have explained it a little better here. Like I just told you here about the process of schedule.
Whenever you do set state, react adds update to an internal queue. Basically, it updates its task in the internal queue. Like one task is already there, the second task is here, the third task is here. It will keep happening one by one. First in first out. reactivate until The current event handler finishes execution. That means, I have just told you that this is your current event. Now, this will not work until the entire event is completed. One event that you have just clicked, the entire event will be completed. In this, it may be possible that you have also done the task of finding and dimming. So, the entire event waits to be completed so that it can batch multiple updates. Like here, you may have set the state in all the events in four times. During render, then react decides when to re render the component and when your work is done, then it decides when the component will be rendered. It will re render basically, it completely decides react, you can't do anything in this. During render, react reconstellation, basically we call it diff algorithm, the same algorithm, real dom, virtual dom, it will compare in it, it will do necessary changes.
If I want to work on current state and I want to do four times increment then how will I do it? Again we will do the same functional update work here. Instead of doing this we will do functional update here. Functional update works on your current value, on current state. Like here count plus one is one, then your state one is one, then it will work on current state. This is a special thing about functional update which I didn't tell you. So here you have previous value. If you don't return, then it's a mess. And I removed the curly braces instead of returning.
So it is working on the current state. The first update here, the value of current state is 1. So it is working on the current state. Functional update works on current state value current state value means the value which has been changed will work on that so this is your solution of both this too and this too and rest if you want more according to that use effect hook which we will read further I hope you have understood this much and if there is some confusion left then you can repeat it.
Is your event, it will be executed completely. Then you will see how many times your setter function has been called here. It will batch it once and the old state will be working on the same state if you want to work on the current state then here you have to use the functional update syntax ok I have told you about the async nature, the synchronous code will be completed like this and after that whatever the updation will be and the rerender process and the schedule will be done internally and the diffing algorithm will definitely be done whenever the rerender will be done The concept of diffing is running. It is comparing virtual DOM and real DOM. It is changing the necessary part. And the whole mechanism.
And one more thing you have to keep in mind. The state where you have used. Which I had told in the last lecture. You can do props drilling. You can send from parent to child. So wherever you have sent this state. That component will run that mechanism again. And your state will be updated there.
So now if you look here, most of the beginners, Java script is not strong, so most of the beginners get stuck in it. So all these things, if I tell you now, then the concept will gradually become clear to you. And if you do not do this now, then you will go to this. So if you do this, then there will be an error. And if you do count plus plus, then definitely this is working. Wait a minute, I will comment all these things here. So come here and oh man. So this is working properly for us. If we do count + + here, then this does not work for us. I don't understand why people are teaching this in react. This is a basic thing. I will show you by making it work. Assignment to constant variable. If it works here, I will let it instead of const. It is working. But still it is not working properly. See, I have clicked once here. But here it has not been incremented. Why? Because here we have done post increment. If we do pre-increment, plus plus, then it will work here. Let's increment, see. Okay. So now you must have understood why it was not working before, why it is doing now.
See here, we did const. This is the part of normal java script. If you have made a constant variable, then you can't change the constant variable. You can't assign value to it again. And this post increment or pre increment is assigning value to that variable. So assignment work is done. So how can you assign value to a constant variable? It is giving you an error that you are assigning value.
So increment is not done, then I will increment, so this old value is incremented here, but in the actual variable now you will have 2, because it has been incremented twice, ok, so here you should pre-increment, if you count plus plus, then it will work properly, let's reload, then it is working properly here, but you should not do it this way, I told you to use const to avoid accidental change, it may be possible that you update it in count only, then it will not reflect in the UI, So accidental. To avoid changes, use const here. And here it will not work. So here, count plus one. This is how we are using it here. I hope you understood. Otherwise, things are going on YouTube. So you have to understand a little. If you have done javascript well, then definitely you will understand a little more by explaining. Otherwise, you will need a lot of explanation. So now it is working properly. So I believe you must have understood all the things. and you must have enjoyed it. Did you enjoy it?
Starting portion, I just checked, basically, the break I took, I checked how the sound is coming. So, I connected it wrong. I can't even show you this pin right now. I connected it wrong. Basically, I use two pins here. I put it in the wrong one, so the sound again He was taking our mobile, so in the starting portion, the sound and noise would be more. So there are two or three devices, it seems very confusing to do here.
