- Podcasts
- ReactJS Developers Podcast
- React useRef Hook Explained – Common Mistakes, Practical Uses and Best Practices
React useRef Hook Explained – Common Mistakes, Practical Uses and Best Practices
ReactJS Developers Podcast
- December 3, 2026
- 44 min
- Episode 6
About this episodeIn this episode, we explore the useRef Hook in React.js, an important React feature that developers often misunderstand or misuse.The…
- What the useRef Hook is.
- How useRef works in functional components.
- Understanding the .current property.
- How to access DOM elements using useRef.
- Managing values without triggering re-renders.
- The difference between useRef and useState.
- Using useRef for input fields and form interactions.
- Storing previous values between renders.
- Common useRef mistakes and how to avoid them.
- When not to use useRef.
- Best practices for using React Hooks.
- Beginners learning React.js.
- Frontend developers working with React Hooks.
- JavaScript programmers.
- Software engineering students.
- Developers looking to improve React application performance.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
React useRef Hook Explained – Common Mistakes, Practical Uses and Best Practices
Now in this part, we are going to discuss the use reference hook, which is a very interesting hook. Maybe you have already explored it, but there must have been a little confusion about when to use it, what happens, why to use it. So all the things will be clear in this part, I guarantee you. It will be very basic and I will cover all the aspects. So first of all, you read a little definition of what is written here, what happens. Although after reading the definition, you will not understand anything. But still, read what is written here. What is reference? Before hooks, ref was used with class components to directly access DOM elements and React elements. DOM elements or React elements, if you want to directly manipulate or access them, then you use ref first in class components. But are we learning about class components? Are we in class components? Not at all. What are we using in React? Functional component. So, for functional component, you get use ref. Ref used to be used in class components, you can go and see, but now you don't have to use class component anywhere, you will get it in many legacy codebases. So now we are in functional component, so here you get hooks, and you know that hooks need to be used, so here we are going to use useRef hook. So what is useRef hook? UseRef is a hook that returns a mutable reference object that returns a mutable ref object whose dot current property, listen carefully, what is written here, you will not understand anything, but now when I will show you practically, then all things will be clear, so listen carefully here, use ref is a hook, that returns a mutable reference, mutable means you understand, in which changes can be made, mutable means changes can not be made, mutable means changes can be made, we have already read all this, and read it very well, so mutable ref object, What does it return? Mutable ref object whose dot current property. See here, dot current property. We access the object with dot notation. So, here it means that the current property persists across re-render. This is very important written here. Persists across re-render. I will explain it in detail. We will come back to it. But, first let's code practically. Then you will understand the meaning of this whole line. Okay, this whole definition. Now you may not understand anything. So, let's come to the code quickly. And again, I have installed VIT. You can do VIT React here, which you have to make from whatever, but VIT is still running. So, here I have installed VIT and properly set up Tailwind CSS. Because I think there is a need to design in this lecture. So, you can also do it with CSS. There is no problem. I have set up Tailwind CSS. See, I will show you. Here I have imported Tailwind CSS. And there are many setups in this. You must have seen. So, do it quickly. If you don't want to do it, then you can do it with normal CSS. But install Vite and work with me quickly, you don't have to see. So here I have done everything, I will remove it in strict mode, otherwise it will confuse you. Okay, you must be seeing things twice. So I removed the strict mode and here I have left the previous normal CSS. Because here you get buttons and styles made up, so I have left it, why do I have to remove it? And I have left the previous styles in app.css as well. I have just added an extra tailwind CSS here, which I can use. I will do it and show you. Now I have made a button here, so you must be seeing a button like this. Click. And if I write a class name here, class name BGRED500, then definitely its BGRED should have been done. But because CSS is written separately here, that's why it didn't happen. Rest, if I remove the CSS button here, then you will know here. You know all this, right? So, there is no issue in this. Let's remove this quickly. Let's remove this too. And use reference hook. The main issue is the same. We get lost in the issue again and again. So, we have to understand use reference hook. What I will do here is, I will take a very practical, small scenario. What scenario am I taking? I am taking an input here. Okay. The type of input is text. No matter. You can give anything now. We are just testing use reference hook. Enter anything in the placeholder. Enter name. This is not necessary. Basically, you make an input box. That's it. And here I am giving a button. Button. I have given it the name focus. So here you can see the input box like this. Here you can see the focus. Now you know that as soon as you click on the input element, it gets focused. It gets highlighted like this. That means it is focused. Okay. So I clicked on it. It is focused. But I want to focus it on clicking on this button. If I click on this button, this input element will be focused. How will we do this? You must be feeling some DOM manipulation here. In JavaScript, we used to do this. First, we used to select the element and after selecting it, we could do our task on it. We could work with EventListener etc. But in React, there is nothing like DOM manipulation. You can do that too, but that is not the right way. Here you know the concept of virtual DOM etc. So internally, to manage proper things, you have given hooks. So here you don't have to do DOM manipulation directly. Here you have to use hooks. Now for this scenario, you have to use the use reference hook. Why? Think for yourself. You want to click on this button. And by clicking on the button, you want this input field to be focused. By clicking on this button, this input field should be focused. So, definitely you will click on this. How will you focus on this? So, you should have its reference. If you don't have its reference, then how will you work? If you have its reference, then you will be able to focus on clicking on this button. Definitely. So, this is what we have to do. First of all, we have to take the reference of the input. How will we take it? So now I have told you that the topic is the same, we are reading the use reference hook. So how do we create the use reference hook? So like normally you use state etc. All the hooks here are almost similar to use all of them. So here you have to make a normal variable. You can give any name. I am going to put this input reference in it. So I give the name input reference. Rest, you can give any name. It's your choice. Okay? Is equal to use ref. And it will automatically import. Look up there. It's imported. Okay? Here, you have to call like this. Just like normally, you use all the hooks. Use, state, use effect. You have to do it in the same way. Here, we have called. That's it. Has our input reference been created? I won't say exactly. Because, if I click on here, and I make a function. Handle, focus. Okay? And here, I I have passed a reference. I will make a function here. const handleFocus. I have made a function like this. And here I will console and show you what you have in the input reference. Understand carefully. I have not done much. I have created an input reference using the user reference hook. And I have put onclick here. I am running handleFocus. So this input reference is consoling here. That means I want to see what is in the input reference now. Okay. We will learn what is in the starting and what we are going to do in the future. So, as soon as I clicked on focus, an object is coming here. As I told you, it returns a mutable ref object. So, we are getting an object here. Okay. We are getting an object. It is mutable. We can change it. We will see that later. But, what are we getting here? We are getting an object. All right. Here we have a current property. We had read that whose dot current property persists across rerender. So remove persist across rerender. We have got dot current property. So we are getting dot current property here. That means we can access it with dot current. But what will we access? It is undefined. Look carefully. It is undefined. There is nothing. We need input reference. Now where will input reference come from? Come to code here quickly. So for now, as we have heard in the definition, we will get an object here. We will get an object. In it, there will be a. current property. Yes, there is a property too. We don't know about the future. We will find out later. So here we need an input reference. We need this reference. So how will this reference come in it? So there is a very simple way here. Copy this variable from here. Here you have an attribute. You can call it prop of ref name. This is in the class. No, it is not that. Here you can say ref attribute. You can say prop. So ref. Here you have to put input ref. Now near input ref, you have this input reference. Really what? We just put this input ref inside ref. And then near input ref, its reference came. Yes. See what I did again. I saved for save. Input ref is entered like this. And in input ref, you will see that the reference of the input has come. Okay. I click on focus. I clicked on focus. Yes. First it was undefined, but now input is coming. Okay. What happens if I here, this whole, Let me apply this whole thing here. If I put the input reference in the button, then what will happen? So here you will get the button reference. Let me click on focus. The button reference is here. So in DOM, we use the getElementById or something querySelector and select it. So here it is done like this. So here you have the reference. So whichever reference you want to give, simply make a variable from useRef. So, we have got the reference of input reference. Now see carefully, what is there in the input reference? Current. There is that input in the current. Okay. So, dot current. How will we access? Dot current. Dot current. Alright. So, I click on focus. See, input is coming. I am hovering on it. Here, input is visible. Okay. What we had to do? We had to focus. That's it. We had, like we used to select input from gate element by id or select any element. I have selected. I have selected. Now, if I want to focus, then dot focus. It is so simple here. Dot focus. Okay, now you see here, I reload. I clicked on focus. Input field has been focused. Input field has been focused. Okay, you see. I clicked on this focus. But because I had input reference, I have focused input field. Yes. I will give you one more example. You will understand properly. I will remove it from here. I will leave button. I will make a simple div here and I will not do much in div. I will give class BG red 500, width 40 and height 40. And you can't see anything like this here. Handle focus is not defined. Okay. We have deleted the function of handle focus. No problem. Let's make it. Handle focus. This is our function. Let's come here and reload. So what do you see here? Square is visible. square is visible and a button is visible so now I want to click on this focus button or remove the focus, just click here click click and handle focus, handle click, by the way nothing is kept in the name but it looks good, let's go to the sequence handle click, so now I want to click on this button to make this whole circle, now it is square, make it a circle we could have done this by selecting in java script and applying style to style and we can do it here also in exact same way Not the same way, but after selecting, the method is the same here. So how to select? By using the use reference hook. Now you will know how to make a reference. Simply give a name to any variable here. I give the name div reference here. And I will use the use reference hook. I will call in this way. By default, it will remain empty. If I console it, it will be a dot current property, but it will be undefined. So what I have to do? is equal to div ref. Now, in this div reference variable, you have got the reference of this div. Okay. Really? Do CLG and see. Div reference. Let's reload and click on it. You have a div here. If you see the dot current here, then you will see the whole div. Click on it. Now you want to see the style from the dot style. If you click on it, then you will see the style. There are many things, but now everything is empty. Some background color will be given. Isn't that too? Okay. Let's try something else. Dot style. Basically, we can change it now. Dot style dot background color. Okay. Who will go to find that much here? There are a lot of things here. It's very cold. So, div reference dot current dot style dot background color. Because we know that background color is given here. Dot background color. So, what background color do we have? Let's try. Let's click. Nothing is coming. I don't know why. The background color should be visible. The style should also be visible. Let's try to see the class name. Let's reload. Let's click. So, yes, we can see the class name. Maybe we are using Tailwind. That's why something is happening here. I don't know. Here we can see the class name because we have the class name here. Okay. So, all these things are coming in the class name. But I have to change its style. Can we do it? Can we do it? Definitely. We can do it. So here, div reference dot current dot style dot background color I want to change. Background color is equal to I want to make it red and blue. Okay. So I reload. As soon as I click, it should be blue. Wow. It is blue. Wow. So, as soon as I clicked, it turned blue. That means I can do style work here. I can also add different events here. I just need reference. I can do all the work on it, which I could do in DOM manipulation. Alright. And what else can I do? Let's say I want to make a circle with this. So, div reference dot current dot current dot style. Wait. border-radius is equal to 100%. Let's see if it works. If we click, it becomes blue and round. This is how our user reference hook works. But it's not that much. You can learn this in other places. You can understand it by reading the documentation, but it is not that much. There are many things in it that I will teach you now. And that is more important from the point of view of the interview. Okay, so let's come here. I will give you these notes first. This was the basic syntax that I had to explain to you and I have explained to you. If you want, you can also apply click on this div. I will tell you how here. So here I put on click. Let's say I am doing CLG here. I have clicked on the div. So now you know that if I click on this div, then I have clicked on the div. But if I click on this click, then will it click on this div? If I click on this click, then it will be round and blue as we have done now, but it did not click on this div. Obviously, if we click on this, then how will it be clicked? But now we can do it. Now we know that we have its reference. So I can do div reference dot current dot click here. Okay. Let's reload. I will click on it. So style will be applied. Along with this, click will also be done. So as I clicked, style will also be applied. Along with this, click has also been clicked on this div. So we can do this kind of work. So I will give you its notes. control C and here one folder will be made of our notes and inside the notes here will come to us basic syntax dot gsx alright ok now what do we have to do so now let's go ahead and see what other things we have here I will explain this just now property persist across rerender and this mutable ref object I will explain everything so here We have understood the basic syntax that we can work in this way. Use ref is like a box. You can store anything. It's not like you can store only DOM node or reference. Anything. Anything means you can store number, object, it's your choice. But why to store this? We can store in use state too. So why to store here? So this line written here, Persist across re-render. This is why we like to store here a lot of times. And I will tell you this right now. You understood the basic DOM, right? So I will tell you that too. You can store anything in it. Changes to dot current do not cause re-render. We will discuss this just now. Now we are coming to this. So changes to dot current. If we change something in dot current, then re-render does not cause. And when re-render causes, I will tell you in use effect. Sorry. You must have seen in the use state that as soon as you update the state, the rerender trigger happens. I'll tell you now, I'll repeat it once again. It's not reactive but persistent. This whole line will be clear to you as soon as I code. So here you have two main uses of use reference. First of all, accessing DOM elements. to access DOM elements, to do some work on it. So, we have seen this now. Then the second one is storing mutable values without re-render. This one is left to see. And as soon as you will see this, its definition will also be clear. Its definition will also be clear. And these three lines written here, this will also be clear. I will repeat it again. Okay. So, what do we have to understand now? We have understood this accessing DOM element. Now, if you have a video, then try this example. Rest, I have told you from the input. I have told you by clicking. And what else do we need? You can do all this. You can access DOM elements here. Let's understand this example. Now, we will not understand this example directly. Let's warm up a little bit. I will tell you what things are happening here. I have given three notes, right? Yes, I have given. So now, just listen to me carefully. And you tell me. Till now, I will ask you a question related to what you have studied. What happens or what does not happen? You have to tell. After that, you will understand it yourself. So here, I am making a state. I am giving the name of the state, count, setCount. There is no issue here. I have given count, setCount. Now I am making a button here. I am giving the name of the button, increment. And you know, I have given the name increment. That means, we will call a function here and increment it. Wow! so onclick handle increment function here. const handleIncrement Alright. HandleIncrement is made. Very good. What will I do here? setCount countPlus1. These are all children's things which now you find very easy. Okay. So I have incremented count and I will show you by adding an h1 tag. Alright, so let's come here and reload. As I will increment, it is being incremented. What are you telling? These are basic things. What is the use of this? Why are you telling this? I will tell you why I am telling you. So it is being incremented after incrementing. It does re-render cause. If you have read the lecture on useState carefully, then you will have such an idea that as soon as we click on increment, sorry, we don't click on increment, basically, whenever we do setState, we use the other function, setCount or setState, whatever it is, then it does re-render trigger. Now, how will it know that it does re-render trigger? So, put a useEffect here. You know when useEffect runs. If you have given dependency, then it will run according to dependency. And if you have not given dependency, then whenever re-render trigger will happen, then only this useEffect will run. Meaning, Let me do CLG here. It has been re-rendered. Now look carefully. When will this useEffect run? Whenever you know that something changes in the state, it re-renders. After re-rendering, useEffect runs. If I give dependency, then it will run according to dependency. And if I give empty array, then it will run only on load. When it will load for the first time, then it will run. That much you know. For now, it will run every time whenever re-render trigger happens. And when does re-render trigger happen? When it changes in the state. When? and we set the state by using the setState function. Then the re-render trigger happens. As soon as the re-render trigger happens, this use effect will run. First I want to explain re-render. I clicked on increment. state changed and re-render triggered because we have used CLG in useEffect so we can see here re-render has happened I will increment again, change to state and re-render will happen I will increment again, change to state and re-render will happen re-render will happen, then only values will be updated everywhere if I have used these values 10 times not 10 times, 3 times only then everything will be updated as soon as I click on increment Because the re-render trigger has occurred. Let me increment again. The state has changed and the re-render trigger has occurred. So, first of all, understand that whenever we change in the state, using the set state, the re-render trigger occurs. If the re-render trigger occurs, then the entire component will be re-rendered. The entire component will be re-rendered. Is this clear? Yes, this is clear. Now, let's talk a little about use reference. So, this re-render trigger, when we change the use reference, basically, if we do some changes in the use reference, then the re-render trigger doesn't happen. I had written this there. Okay, do it and show. I'll show you now. So, let's make a const count reference using the use reference and give 0 here. Now, I told you that only to keep the reference of DOM element or to keep the reference of React element, we don't use it. Here, we can also put beautiful values. We can add numbers, objects etc. So, I have added a number. What have I done? I have added a number. Now, we have seen that if we change it in the state, then it gets re-rendered. Now, let's see that if we do count reference dot current plus equal to 1, then does it trigger the re-render? Did you understand the meaning? I just showed you what will happen in the count reference. There will be an object. There will be a dot current property. There will be a current property. What will happen in the current property? It will be zero because we have given zero. If you can't believe it, then see it. Do it yourself. Count reference. Count reference. Let's come here. Let's reload. Click on increment. See, current is coming. It is zero. So I can see the current only from the dot current here. Let's reload, click on increment, here 0 is coming. So now I want to change in this reference. I don't want to change in state, I want to change in reference. So count ref dot current is equal to, sorry, plus equal to 1. That means I am doing count reference is equal to count reference plus 1. Means every time I click on handle increment, it will be one plus. Can I see? Yes, in this place, I am I can see count reference dot current. Now why do we have to look at this place? We could have done it here too, we could have applied it here too, but there will be no re-render trigger. There will be no re-render trigger, according to definition. Now I told you that if we change something in the count reference, then there is no re-render trigger. It does not react. There will be no re-render trigger. Now there will be no re-render trigger, so this place will not be updated either. So we will have to look inside the function that whether it has been updated or not. Rest, there will be no re-render trigger. If there is a re-render trigger, then useEffect will also run, but now it will not. Now see carefully. So I reload. As soon as I clicked on increment, 1 came. I clicked on increment, 2 came. That means, in useRef.current, we are updating, but after updating, as in the state, re-render trigger was happening, here re-render trigger is not happening. Otherwise, here re-render has happened. This kind of console has come, because we have put it in useEffect. So re-render trigger, It is not happening. Okay. Now you should get a question which is an obvious question. Definitely you should get it if you haven't got it. The question is that why should I use this? I can make normal variable also. Normal variable also doesn't do re-render trigger. You have also read this in use state. Yes. In use state part, you have also told that normal variable doesn't do re-render trigger. So if re-render trigger was not to be done, then normal variable would have been used. but it has a demerit. What is the demerit? I will make a normal variable here. Count where is equal to 0. I made a normal variable. I commented it. And I also commented this. I will do it below. Count where plus equal to 1. Whenever I click on handle increment, I am doing plus 1 in this variable. And definitely it will not do re-render trigger. So we can't put it here and see. Okay, so let's console it here. So let's see what variable is being updated. Okay. We are trying to make changes to the constant variable. Let's do it here. We have incremented it. It is working. So what is the problem? What is demerit? Now I will tell you demerit. Look at demerit carefully. I said that whenever we will count set, What will we do? We will set count. Re-render trigger will be there. What will be the trigger? Re-render trigger will be there. So if re-render trigger will be there, then the count variable which we have initialized from 0, it will become 0 again and again. It will not be able to persist the value, right? State persists the value. State persists the value. That's why here, even after re-render, its value remains the same. But if you see the count value, it will become 1 again and again. Why? Because it is getting reset. Whenever re-render is happening, this value is not being able to persist. This normal variable is not able to persist. Once it is re-rendered, it will be 0 again. And 0 plus 1, 1 is visible. Then it is re-rendered again, it will be 0 again. 0 plus 1, 1 will be visible. Why? Because we are using the state. If we change the state, it will be re-rendered. If it is re-rendered, then the normal variable will not be able to persist the value. Re-render is not triggering the normal variable, but it is not able to persist the value. And this is the advantage of use reference hook. It persists the value. Really? Yes. You can see that it persists the value. So I will uncomment it again. Now you see carefully, I am still using set count, re-render trigger is still happening, but this value will persist. That means the old values will be plus 1. Value will not reset again and again. So I clicked on increment, see this is 1 now and re-render is also triggered. But now I will click on increment, so here it will be 2. When re-render trigger is done, value is persisting. Again I clicked on increment, this is 3. Clicked on increment, this is 4. 5 is done. Value is able to persist. Okay. There is still a bit of confusion. Now I will give you more crystal clarity here. Look. Look. Here, here, you have one normal variable. Okay. Let me change the color a little. You have one normal variable. Now this is important, the summary I am giving you, this is important, normal variable. All these things are asked to you and what use cases, I will just show you now. We will discuss the rest of the use cases in the project. But basically we had to understand all aspects of use reference hook, so we understood. There are some more things, I will tell you now. So this is normal variable. Then you get use state, which we definitely use a lot in React. And after that, we just saw use reference hook. So what happens in a normal variable is that it doesn't do re-render trigger. It doesn't do re-render trigger, so what will we use here? Re-render, this, render, it doesn't trigger. We know this, it doesn't do re-render trigger and it doesn't persist either. Sorry, we clicked, it doesn't persist either. in variable, then re-render trigger will not happen. That's why it will not update in UI. And it will not persist. We just saw that it will not persist. It will be reset again if re-render trigger happens. What happens in use state? Re-render trigger happens in use state whenever we update. Value persists too. The value persists in multiple re-renders. It doesn't reset, it persists. This is very interesting. We use useState a lot. UseReferenceHook doesn't re-render the trigger. But the value persists. This chart I have given you is very important. You will not find it anywhere. Note that you do not have a normal variable. It is not doing re-render, it is not doing value persist. Use state is doing re-render, it is doing value persist. We have seen. Use reference is not doing re-render, but it is doing value persist. And the same thing is written here. Now look carefully. Use ref is a hook that returns a mutable ref object. Definitely, we will use this we can change it in ref object and now we have done 0 to 1, 1 to 2 and we are able to change it. That means it is mutable object. Alright. And dot current property persists. Dot current property persists across read renders or renders. Whenever it will render, it will not reset. This value is persisting. So now you must have understood this definition. Use ref is like a box. You can store anything. DOM node or reference you can store number object. You can store anything. Changes to dot current. If you are changing something in dot current, then it will not re-render cause. We just saw that it does not re-render cause. It's not reactive. Not reactive means it does not react. Whenever we change something in it, then it does not re-render trigger. That means it is not reacting. You can go and watch the first video. In that only I told you the whole of react. How does it react? Why is it called react? Because it is reacting. If we change something in use state, using setState, then it reacts, re-renders, values get updated in UI. But here it is not reactive, but persistent. It is persisting the value between multiple renders. Okay. So, you understood this. From two main uses, we have seen the DOM one, how to do it in DOM. Rest, we have seen this one too. But the example given here, we will see this once. Suppose, suppose, we have to make something like this. Should I give it in notes? I will give it in notes. After basic syntax, this was for our understanding purpose. Understanding.jsx. Okay. I have given you these notes here. Very good. Now I will remove it from here. Suppose now we make something like this. Carefully. Listen to the question. Carefully. Wait. Listen to the question carefully. The question is something like this. that whenever re-render trigger happens, let it be, whenever re-render trigger happens, then you have to note the count of re-render. What did I say? Whenever re-render trigger happens, you have to note the count. Means, if it re-renders once, then count 1. If it re-renders second time, then count 2 should happen. If it re-renders third time, then count 3 should happen. How will we do it? So, you will say, is it necessary to use useRefHook in this? Can we do it with normal state also? Try it. Try it. Here I have made const. Okay, how will rerender trigger happen? First of all, this is the same thing. How will rerender trigger happen? So, you know, if you change it in state, then rerender trigger will happen. So, to change it in state, let's assume here some random number state. And set random number is equal to use state. And here random number is by default 0. And let's make a button. And here generate generate random number. Generate random number. What will generate random number? It will generate random number. On click, I will pass reference of generate random number function and make function here. Const generate random number is equal to something like this. And what to do? To generate random number. So, const number is equal to What will we do to generate a random number? Math.random. Okay, this will be used in the next project as well. This which I am telling you here, we will use it in the next project as well. And I have told you in JavaScript as well. Anyway, you can see what we will use in the next project. So, for now, we have to generate a math.random number. So, math.random. Let's do this much. Rest, we don't need to put math.floor and into range. We just need a random number. We will get a random number from this. Okay. So, here, set random number. this number. So, the random number will be set and here we will also show what random number has been generated. In H1, random number. Okay. So, whenever random number will be set, then re-render trigger will happen. You know this much, re-render trigger will happen. Right? Use effect. Here, let's do CLG. Re-render. Re-render trigger has been triggered. Alright. So, here we reload, generate random number, random number is generated, and a very long number is coming, so let's do one thing, let's make it into 100, number will come between 0 to 100, and let's do mat.floor, mat.floor, so here you will get value in integer, you will know all this, if you don't know, then I will explain it in the next lecture, although in the entire series of javascript, I have practiced a lot related to all these things, so you can go and see, okay. now don't pay attention here, if you haven't come here yet, then write this much, this is generating a random number between 0 and 100, ok, so here generate random number 40, re-render trigger happened, again I did generate random number, re-render trigger happened, now whenever re-render trigger is happening, then I have to note its count, that how many times it has been re-rendered, so how will I do that, so here I will make an h1, here I will write re-render count, and here if you want, You must be thinking that it will work with state. Let's do it with state. So render count comma set render count is equal to use state. And here we give 0 by default. And this render count came here. So now what is the render count? It is 0. Okay. What is the render count here? It is 0. Make it a little smaller. Render count is 0. Whenever I do generate random number, then our render count should increase here. Right? So how will the render count increase? Whenever the rerender trigger happens, useEffect will run. Whenever the rerender trigger happens, useEffect will run. Now when useEffect runs, I will setRenderCount to renderCount plus 1. That's it. Our work is done. No, you are stuck here. You haven't understood the state properly yet. As soon as we generate a random number, the random number is set here. When the random number is set, the re-render trigger happens. When the re-render trigger happens, we have incremented the re-render count here. But we have used the state for this too, so it will re-render trigger again. Now if it will re-render trigger again, then it will set again. It will re-render trigger again. It will set again. It will re-render trigger again. It will re-render, the use effect will go on, it will set. Okay, so now if you come here and see, see this. It's going on and on. What do we need here? We need the render count. But to update the render count, we should not rerender it again. Otherwise, one rerender is going to waste. To note the render count, we have to do one rerender. You understand what I mean. You will understand this only when you are watching the sequence. Use, state, etc. I have told you in so much detail. If you haven't seen it, then there must be confusion. One of our render is going to waste, just to do this. So instead of doing this, I will use reference here. render count reference is equal to use ref 0. Okay? 0. And here, whenever re-render trigger happens, render count reference dot current plus equal to, I will do 1. And because re-render is triggered, then definitely I can also show the render count reference here. Why can I show it? Because re-render is triggered. If re-render is triggered, then this will also be updated. So what's the problem? I am doing it in set random number. As I click, set random number will be there. Re-render trigger will be there. Now, if re-render trigger will be there, then value will be updated. Value will be updated and here also value will be updated. Because re-render trigger is there. So, when re-render trigger is there, use effect will be run first and then value will be updated here. Okay. So, come here. Let's reload. So, there is some problem here. Object are not. Okay. Here we had to do use reference dot current. Alright. Let's reload. As soon as we click on generate random number, the random number will be generated and will be set in the state. After setting it in the state, the rerender will be triggered. After the rerender is triggered, the use effect will be run. In the use effect, the user So, our render count will be noted. Along with that, it will be updated here. Because re-render has been triggered because of state. Generate random number. Wow! Now, we are able to note the render count. And re-render is not being done for no reason. It's not like we have to do a rerender trigger to note the render count. No. So that's all you have to understand. First of all, here this value persists across rerender. Secondly, it's not reactive, meaning it doesn't do a rerender trigger when updating the value. These are the two things. If you understood, then use reference is clear. Rest, the DOM manipulation is very easy. Take reference, do DOM manipulation, etc. React element, etc. You can work on it. It's very easy. You will get it everywhere. Understanding this was important here. I hope you understood. Okay? So, you listen again. What happened here? You will know the summary. Something happened here that we changed it to state basically we generated a random number so it changed to state now when it changes to state, re-render trigger happened ok, now when re-render trigger happened so what will run first when re-render trigger happened? useEffect useEffect run happened now inside useEffect if we change it to state again then re-render trigger will happen again and it will get stuck in the loop it will keep on rotating like this that's why we are not using state here so what are we doing? changed to state what happened when it changed to state? Re-render trigger happened. What happened when re-render trigger happened? First of all, what will run? UseEffect will run. We don't want re-render again. We just want to note re-render count. We used reference here. Using reference.current, we put value of render count in it. Now, because re-render trigger has happened, then UI will also be updated. Now, if update in UI, then state value will also be updated here. State and also re-render variable which we have used will also be updated. because it is being re-rendered. So that was it. It was a very important concept which seems very confusing to understand. If you look at the rest of the DOM manipulation, from the point of view of DOM, that we can take references from the use ref hook, we can make changes in DOM, then all that is very easy. But the main concept is made here. And for optimizations, we use these things a lot. This is very important. Normal variable doesn't have a re-render, it doesn't persist. It has both in use state. It does both re-render and persist. It doesn't have a re-render in use reference. It is persisting. Okay. So, you must have understood this too. There were two main uses. Accessing DOM elements and storing mutable values. We have seen this too. And we have seen this one too. How it is working. Right? So here it is written that sometimes you want to store data between re-renders, between renders without triggering update. In the middle of re-render, you want to persist the data, but you want the data to not be re-rendered again to persist. So you can use the use reference hook here. That's it. I have given the difference between use state and use reference hook. What is the difference? Trigger re-render, so use state does it, use reference does not do it. Value persists. Yes, it persists. This also persists. Yes, both persist. Re-render trigger basically does not use reference. It does this. Common use is store UI data, store DOM reference, and mutable values. That's it. Mutable values, that means what can be changed. Thank you so much. This was ours. If you have understood use reference, then next is going to be a project. And you have to make that project yourself. And if it cannot be made, then definitely take help. Don't worry at all, because as a beginner, things are not being made. I also know. Let's meet in the next project. Until then, bye bye and keep exploring. Thank you so much.
