- Podcasts
- ReactJS Developers Podcast
- Working with Radio Buttons in React.js – Handling User Selection and Form State
Working with Radio Buttons in React.js – Handling User Selection and Form State
ReactJS Developers Podcast
- December 3, 2026
- 50 min
- Episode 8
About this episodeIn this episode, we explore how to implement and manage Radio Buttons in React.js.Radio buttons are essential form elements that allow…
- What radio buttons are and how they work.
- Creating radio buttons in React components.
- Understanding controlled and uncontrolled inputs.
- Managing radio button selections with useState.
- Handling onChange events.
- Grouping radio buttons using the name attribute.
- Retrieving selected values.
- Implementing conditional rendering based on user selection.
- Validating radio button selections in forms.
- Styling radio buttons in React.
- Best practices for handling form inputs.
- Beginners learning React.js.
- Frontend developers working with forms.
- JavaScript programmers.
- Software engineering students.
- Developers building interactive registration and survey applications.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Working with Radio Buttons in React.js – Handling User Selection and Form State
From now on, 2-3 lectures will be based on Form Handling. We will learn Form Handling properly. Whether it is Input Box, Check Box or Radio Buttons. There will be different situations and different strategies. We will learn all those things. Because if you see the multiple big projects, the major complexity comes in handling the data. To handle complex forms, that's where you have the most logic and brain. Because on the database, definitely, until you learn to properly arrange the data, how will you send the data on the server? By listing, basically by ordering, by listing, you have all those things by now. We didn't learn to order data, but the basic overall data, array of objects, how to list etc., we have already seen that. Now how to play with data, how to create your own data, how to handle input boxes, we will see how to arrange all those things in the proper format, what can be the production strategies, all those things. It will seem very easy to you what is kept in form handling, but when you will see now, you will know that there were so many things that were not told to us. And those will be used in big projects. This is the guarantee I give you. So first of all, we will talk about input field. What are controlled components and what are uncontrolled components? How can you handle multiple input boxes with the help of a single function? How can you handle it with the help of a single state? How to make a common strategy? We have to see all that now. So, in 2-3 lectures, hold your heart and learn the form. And we will make a complex form and practice on it. Not in this lecture, but in the next project. In React, unlike plain text, we don't directly access DOM. There is no game of DOM here. I have told you this in many lectures. You don't have to do document.querySelector or getElementById. You don't have to adopt any DOM funda here. You don't have to do DOM manipulation. So how will you select the input box? How will you take the values from there? How will you clear the values? How will you do all these things? So the easiest way here, the best approach is the state one. What is most important in react? I told you data. And what is important to handle data? Here you have state and props. So here you will have the entire state of the funda. If the state is clear, then all these things will definitely be easy for you. You will be learning the strategy. So here we are going to handle through the state. Now if you are linking through the state, then they call me a controlled component. By the name of controlled component, you know why we are calling it controlled. Because we are able to control such components through the state. If you do changes in the state, you will see changes in the component. If you change in the state, you can manipulate and do all the work. So, definitely you will have control over it. What is uncontrolled? There you have nothing to do with the state, you can directly access it. Just like you were doing in DOM, I will tell you a little bit about use reference. How can we work with reference? So, all that will come in the uncontrolled component. And in the interview, a lot of people are asked what is controlled versus uncontrolled? And what should be controlled and why should it be done? Data stays in sync. We have taken a slide about control components. Now we will manage through state. This will sync your data and UI. I have already told you in the state lecture that you have to sync your data and UI properly. So you should definitely know state management. Now we will jump into the code and see how we will do it through state. You can easily validate and modify. You have to validate the email. Whether it is perfect or not. Validate means that if it has put any number in the email, then it is not the format of the email, it is in the proper format of the email. You can put such validation. The number should be of 10 digits. If you want to put all these validations, then if you are working on the state, then it is very easy. You can clear or reset the state. If you have done the work in the state, then it will be very easy to clear the state. The form will be cleared. So work with the state. Make complex forms predictable. You can make complex and complex forms predictable. If you change the state like this, then these changes will happen in the form. You can easily make it predictable. So these are all the things. Now let's see how to do it with a small example. We will also look at other big examples and big forms. So first of all, I have installed the Vite here, you can do it like me and quickly start your work here. So this Vite folder is in app.jsx, I have already cleaned all the things and I have not removed the strict mode yet, so I will remove this otherwise it will be consoled twice if we will see. So it is fine, very good, let's make an input box here quickly and here you must be able to see the output, it is visible, let's give a placeholder here, small things will come to you, I believe. You have done normal HTML. I am giving placeholder here. Enter name. As soon as I give placeholder, you can see it here. Let's zoom in a little bit. Let's open this. What do you call this? Console. Let's open the console. Now what to do here? Now we have to remove the data from here. If I type anything, how will we get this data? And how will we link it with the state? We will link it with the state. Only then it will be a control component. Yes, we have to make it a controlled component. Not yet, there is no link with the state. We have to take out this data and send it to the state. How will we send it? I had told you a little earlier, but now we will go in depth with the basics. So here you have an onChangeHandler. In the event listener, I had told you that you have different handlers, you can do event listener. So here you have onChange, not onClick, onChange. If you have to do something to change, then onChange. So I will apply onChange here. Now it will definitely take a function. So we are giving it a function in this way. What did I say? We will apply onChangeHandler. It will take a function. Now it is your choice. Whatever function you want to give, I have given fattero function. Very good. Now in this function, by default, you are getting a parameter. I had told you. You are getting an event object. By default. You give name here. Give event. Give object. You are getting an event object. Whatever name you give, you will receive an object. Basically, you have to receive. You are getting an object here. So by default, If you are getting any parameter here, you can give it a name. I will give it an event name. You can see a lot of things in it. Let's reload as soon as I type something. There are a lot of things in it. It is basically an object. If you want to know in detail then go to dom manipulation lecture. You can explore a lot of things there. So what we have to do here is that we just need value. So we will use event.target. Now what will happen with target? We made a mistake. So, what will happen with event.target? e.target basically. Whenever we type, it will be a target wherever we are typing. So, what is our target? This is the input box. You can remove whatever you want from it. For example, I want value for now. So,. value. What did I say?. value. You will think that this is an object that is accessing with. value. So, behind the scene, you know that your entire document object is converted into a model etc etc. So, this is your entire object. So, this was your object. There was a target in it. We have taken out the target. Now, you will get the value in the target. So, the value came from the dot value. So, will we get the value here? Definitely. I wrote A. I am getting the value. B. I got the entire A and B. C. I got A, B, C. Whatever I am writing here, I am getting that. I have emptied it. I am writing minus. Here, minus is coming. Wow! Our data is completely linked. But it has not reached the state yet. So, it is very simple to reach the state. What to do here? Const. We will make a state here. Let's name it as state. State, set, state. Like this. Oh man. Is equal to use state. And by default, it will remain empty here. So, what will we do here? Instead of console, we have to do set state. I messed up. Set state. Instead of console, we have set the state. E.target.value. Whatever the value is coming, it will come in the state. If you will console this state, then you will definitely see it. I have consoled the state. Reload. I have written m, m is coming, a is coming, m is coming, n is coming. Whatever I am writing, you can see it here. Now it is linked with the state. Now it is linked with the state. So whatever we will do in the state, that will be being splitted with your data and your result will also be visible here. For example, I take an H1 tag here and render this state here. So whatever I write here, you will see it here. I wrote minus, I wrote Kumar. Wow! Okay. Now I want the normal functionality of the reset form. I want to reset the data. How will I do it? I want to have a reset button on which I click, then the data will be reset. How will I do that? The meaning of control component is that we can control the entire input value. So definitely we have to learn that. So we will take a button here. OK, I have given clear. Now what to do here? On click. You know, if we change it in the state, then wherever the state is used, it will change. So what I will do here is that I will empty this state in the set state. What is coming here? I will empty it completely in the set state. As soon as you click on the clear button, what will be set in the set state? Empty value which was the first. It will be completely empty. Okay, come here. Let's reload. And some error is coming. Let's see what error is coming. Too many re-renders. Oh, I am so sorry. I am so sorry. Here You have to pass the whole function or pass the reference. You don't have to call directly like this. I had told you this before, so let's reload. And the button should come down a little bit here. So let's use a VR tag. So this button will come down like this. Use another VR tag. It will come down a little bit. Okay. So here I am writing. All right. And. I am not going to do styling here. I have written minus kumarlal here. All is written here. As soon as I clear here, it should be clear. This should also be clear. Because state is clearing, so I have written clear. It is clear at the bottom, but not at the top. How? Because you must have read in HTML that there is another attribute here, value attribute. Whatever you give in value, you can see it here. Whatever you give in value, you can see it here. Suppose I give mkl in value. Now I have given mkl here, so you can see that mkl is fixed here. Whatever I am giving in value is coming here. Now if I want to type something here, There is a change in the state, but not here. And the changes are happening in the same way. The value is changing with the value. Once I typed, the state has definitely changed. But the value was fixed. So, it is working on the old value. It is not working on the new value. So, I fixed the value. I don't have to fix it. So, we need the value in the state. So, we won't do much. We will put the state in the place of the value. Now, it is dynamic. The value will change. Sorry, the state will change. And this state will be set on the value. Okay. Now see what happens here. I have written minus. It is changing in the state. And the value is also changing. And the state is also changing here. Now if I will clear then the state will change. If the state will change then the value will also be empty. If I will clear then the state will be empty. If the state will be empty then this will also be empty. I have linked the value with the state. I will clear. Everything is clear. So in this way we are able to control our input component. Okay. That is called controlled component. And this is asked a lot in interviews. What is controlled component? And what is the difference between controlled component and uncontrolled component? You have to tell all these things. Okay. So, I hope you had fun. So, this is all. Now, let's see what else we have. We have to discuss more things in depth. Now, wait. We have just started. So, here type something. Our pen tab has gone again. Bye bye. Tata. It's a great paint. It's killing me. It's working with a mouse. Leave it. It's not a problem. I haven't written much. So, pay attention to the value attribute. Don't forget to connect the value with the state. Otherwise, there will be connectivity. How will you control it? You have to connect the state with the value so that you can change it in the state. So, if you are doing clear in the value and changing it in the state, then you can see it in the form too. And onChangeHandler, you definitely have to install. Otherwise, how will you take data? Okay. Without onChange, the input becomes read-only. If you don't install onChange, then your input will remain read-only. If you are typing something, then the data is not coming. Right? So, very good. Let's move ahead. What else do we have to do? So what is control components? If you are asked, then a control component is a form whose input field value is controlled by react state. A control component's simple form or input field or whatever you call it, is a part of the form. Where input field value or whatever checkbox, radio button, whatever you are using, that is controlled through react state. And you link it with react state. So working cache store input value in state. Save the value of the input in the state. Use the onChangeHandler to get the value. And link the state and the value attribute. Attach it. That's it. The control component is ready. How to do uncontrolled work? There was a small DOM manipulation. In DOM manipulation, a lot of garbage had to be spread. So, something like DOM manipulation, I will tell you later, it is not very useful. In a very rare case, if you get something written, then it is not recommended in that way. This is the proper way to do it. But I will tell you that too, so that you can know the difference between a controlled component and an uncontrolled component. And you can give a proper answer if you are asked somewhere. Or if you have seen it in the code. So, what is an uncontrolled component? I took this slide earlier. I had to teach more things earlier. So, let's read multiple values. So, there is not much. So, what is uncontrolled component? Uncontrolled component we are not managing through state. Controlled component we have to manage through state. Uncontrolled component means we don't have to manage through state. We don't have much control. Because we are not managing through state. How will we splash in data? Okay. So what we do here, we take a reference here. Like document.getElementById, from query selector, you used to get a reference of input field, from that you used to get value, and whatever work you had to do, you used to spread it. So here also you have to do the same, but a little different method, DOM manipulation, so React. Dom manipulation is not needed. I am giving you a userf hook, use it. We will read userf hook in detail later. For now, I will just tell you how this works. So, let's do one thing to give you this code. Here again, I will make a notes folder. And here I will write basics.jsx and I have pasted it. And here we learn our new things. Very good. And do app.css link quickly. import app.css. Very good. Otherwise, the button will not show css. Okay. So, now what to do here? How to work in uncontrolled component way? Come on. So, here I have again made an input field. Okay. Very good. Here I give placeholder, inter name. Okay. Alright. Now I need value from this. So, how will I take value? Now you don't need to do all the changes to get the value. You just need to work in uncontrolled way. So here I put a simple button. multiple elements can't be returned in one go so I have to return one element in one go so I wrapped it in a fragment and here I say submit I made a button and here on click on submit function here in this way function submit I am working in this way I made a submit function and I put it here so on click this submit function will work let's do CLG So, this is all basic. You know this. It will work. Why not? I will submit. It worked. Alright. And here do this. What should I do? Give a line break here. So, something will be coming like this. Great. Okay. Alright, I want to enter, submit and get the value. Nothing much, you have to use the use reference hook. It is like a normal variable. Make it a variable. The reference of the input, like document.querySelector and getElementById, if you save it in a variable, it is the same. I want to save the input, so I give the name in the input reference. You can give any name. I will use use ref. Reference. If you press enter, it will be imported automatically. Just like the use state and use effect, the use reference has been imported from the core react. You have to call it like this. By default, the reference you have to enter in the starting, you have to enter null. It means that it doesn't have any reference. Now it doesn't have any reference. Who do we have to select? We have to select the input. Listen to me carefully. Sort of DOM manipulation. Now I have made a variable here which has no reference. It is null. I want to give it an input reference so that I can work on it. I want to give an input reference. The method is very simple. Here you have an attribute named ref. Reference. Okay. Here you don't have much. You have to pass the input ref variable. And your reference has come in the input reference. Are you telling the truth? Yes. Really. Do CLG and see if you have input reference or not. Input ref. Ok. Nothing much. We have made an empty input reference. I said I want an input reference. I have to link it. So I have put an input reference in a reference attribute. Your entire input reference is inside the input reference. Ok. Let's see. Come on. Submit. Look at this. You can see what you have selected in the current. If I put it in the reference button, then the reference of the button will come. What's the big deal in it? The reference of the button will come. Look, I submitted it. Who is in the current? Button. There will be a lot of things in it. Look here. Okay, so you don't have to go in that much. Who is the current selected? Button. So here you have to put this in the reference you want to give. It is very easy. Okay. I gave the reference of the input. Okay. So how to do the work? The work has to be done in such a way that if I want value, then input reference dot current. Did you see the current you were getting? Oh, man, look at the whole input reference. What are you getting? I submit. There is a current key in the object. Do dot current. OK, then submit. Now there is this whole input in it. It needs value. Like you get the whole input from event dot target. So you need value. Here you got this value. Reload. Write something here. Submit. Got the value. But we don't use this method. It's not a very controlled manner. It's not a very controlled manner to show. Are you able to understand what I am saying? Maybe you are able to understand or not. As you will go in projects, many things will be mixed and used. So there you will understand that this is the benefit of state. That's why we combine it, make it controlled with all things. So that we can manipulate the data according to ourselves, validate etc etc etc. I will tell you everything in the lecture of user reference hook. Where to use the user reference hook, how to use it, how the whole DOM manipulation is happening with the user reference. I will tell you everything. So, you don't have to read much on it. How to use the uncontrolled component. You know about the state. If you are changing anything in the state, it will be reflected everywhere. You can manage everything properly. We will not be able to do it here. If I tell you too much, you will get confused. So, leave it for now. Alright. How and what we can't do. I will tell you everything, don't take any tension. So, we can work in this way. We have passed the reference here. You are getting the value in the input value. And if you want to show the value of the input reference dot current value. I am giving you an example. I have written H1 here. Input reference dot current dot value. Now you see what will happen here. Look carefully what will happen now. Input reference dot current dot value, your value is coming. Reload kiya mainne. Abhi dekho yaha pe kya aara hai. Cannot read properties of null. Starting mein toh koi value hai hi nahi. Starting mein apke paas koi reference bhi nahi hai. Thik hai. Toh yaha pe input reference mein aise tarike se question mark lagao. Chaining karo. If there is a question mark then it will be visible otherwise there is no error. So there are a lot of things here. So definitely I don't want to tell you that a lot of things have come. I have to teach more. So here I have to tell you. I can only teach you. I have to tell you. So here I write hello and I do submit. Here hello is coming. It is not visible here. Again, you have to save it in the state. Then you will be able to do some work. This is what I want to tell you. You don't have much control here. You don't have much control here. So, you take it in notes. Rest, you do whatever you want to do. Basics.jsx is done. This is our uncontrolled.jsx. And basics was the same controlled one. Controlled manner, you understood. So, here rafc and here import app. Let's move on to the next important thing. How to handle multiple input fields? Thank you. So, how to handle multiple input fields? Now, the last one is left. So, I will tell you how to do this. You will think that we will make multiple states, we will handle it. But here that approach is also wrong. We have to work with state. But to make multiple states for multiple input fields, then there is a little problem there. So, let's see how to solve it. Come on. So, here, I have created an input field, I have given placeholder, here I have written enter email, I have created another input field, I have written enter phone number, I have given enter name, I have given name above, I have given email below, ok, name, email, very good, alright, and here wrap it in a fragment, quickly wrap it in a fragment, and give line break here otherwise it will be in one line only. So, come here. See, you have two input fields. Now, you have to take the values of both. How will you take? How will you take? How will you take the values of input field? So, here we have to make two states. I will tell you the correct way but for now, what you are thinking. We have to make two states. First, we need email. So, email, comma, set email. We need email. We have set the name below. Set the name above. Although, it doesn't make any difference now. But it's okay. The name is above. So, setEmail is equal to useState. Alright. And you will get one name and setName. Okay. And by default, there will be empty values here. Okay. Alright. So, what we have to do here is to put onChangeHandle. If you don't want to make a function here and want to make it outside, then you can definitely make it. And what is the issue here? Function and here name handler is equal to this is your function. Now pass the reference of this name handler here. That's it. Now you can handle whatever you want to handle here. Oh, I'm so sorry. I mixed both normal function and fatter function. Where does this happen? This is what we do here. So, here we have made a function and we have passed a reference here. Now, whatever work we have to do, we have to do it in the function here. So, this is the way you can also do it there. There is no problem. Now, from where will E come here? will come from where it was coming from. From here you will pass E here. So by default you will be getting an event object. Give the name E, give the event. And do CLG and see that you will be getting E dot target dot value. So whenever you change, this function will run and you will be getting value from target dot value. I write anything. So here you are getting value. All right. What to do with this? Save in state. Set name. Save in name. In the same way we can make an email handler here. Email handler. All right. And here we will pass the email handler in onChange. So, you can see that we have to make different handlers. And in setName, we have to do setEmail. If anything else happens, then we have to make more handlers. You must be getting the values here. Let's do CLG, name, email. Let's come here and reload. Here we have put the name, we are getting the name, we have put the email, we are also getting the email. Ok, I put minus in the name, I put mkl at the rate gmail.com in the email, so I am also getting the email. And if you want to reset something, then there is nothing to worry about, make a button here and in the button, let's do one thing, let's show the name, email here on the UI, what is the problem? Take a normal paragraph here. And in this way, show the name here. And show the email just below it. Okay. And here we have to make a clear button. Here is the clear button. What to do on click? So let's make a function here for this. If you want a clear one, then there is a function. If there is a function above everything, then let's make it here. So clear handler is equal to... We don't have to take E, we have to do clear only. What will we do with the event object? So what to do here? In set name, empty the name in this way. And empty the email in the setEmail. Very good. And we will pass the reference of clearHandler by not giving the function here. Now it is doing the perfect work. Let's reload. Clear is stuck here. So do one thing, put it here too. Very good. So, we put the name as minus, put the email as mkl at direct gmail dot com. Now, again, if we clear here, what will be clear and what will not be clear? Tell me quickly, quickly, if you are looking at it correctly. So, here definitely your state will be clear. But we have not linked the state with the value. So, this will remain as it is. This will be clear. This will remain as it is because we have not linked it with the value. So, I will clear it. It is clear below because the state is clear. But we have not linked the state with the value. So, this is not clear. So, quickly link it with the value. You don't have to do much to link with the value. Put the name in the value. Many times we make this mistake that we put email in the input field of name and put the name in the email field. See how the bug comes. I will show you. I put the name here and I put the name state here too. You know that if the state changes, it will reflect everywhere. I am writing the name here and the name is being written in the email too. So, you don't have to do all these things. And sometimes it goes upside down. For example, here is the name handler. So, here we have put the email and here we have put the name. Now you must be writing here. Let's reload. We are writing our name and writing it in the email. So, we don't have to do all these things. Now, why I am not writing the whole thing? Why I am writing only one letter? You can definitely give the answer to this. It works with the current value, right? So, you wrote A. In the email, A came, but in this, A did not come in the name. In the name, it is still empty. Now, if you write something, then with empty, if you are writing something, say, B, then it will go straight to B, right? Did you understand what I said? So, you don't have to do all these things. I told you that this happens sometimes. So, name, email. Here, I wrote name minus. E-mail written, hello and cleared, everything is clear. You can handle multiple states in this way, but you can handle multiple input fields in this way, but you can handle multiple input fields in one form. Now you will make multiple handler, you will clear, you will make multiple states, then it is a waste in a way. So you don't have to work in this way. How to do it? You have to make a single state. Const. We will create a single useState. And you know that if you want to store multiple things in key value pair format. Like we want to store the name here. Name is key and value is minus. And here the key is your email and some mkl at the rate gmail.com will come in the value. If you want to store the key value pair then we take the help of the object. We will make an object here. You will get a name here. You can give the key according to you. I will recommend you to give the keys according to the database you want to send. So name and email. It doesn't mean anything from here. You can give this name, email, key according to you. It doesn't mean anything from here. Okay? Okay. Now, here. See. To handle, you can make different handlers here. For example, there is onChange here. So, in onChange, by making a function like this, you are getting previous data here. Let's do it like this. We have read about the previous data and functional update. Previous data means the object is empty. You will get the previous data. So this is your object. I have told you everything. So here previous data means this object is coming. So now we will change here. So definitely only name has to be changed. It doesn't have to be in email. Only name has to be changed. It doesn't have to be in email. Right? Look carefully at this syntax. I think you are a little confused in this syntax. So first let's decorate it a little. now look carefully here, let's make a function here, nameHandler, nameHandler was already made, let's make one more here, function nameHandler like this, very good, here you will get the value from e.target.value, very good, how to set, this time it is an object, This time it is an object, how will we set it? It is a single state, how will we set it in the object format? If we do set data here, you can also do it in this way. You have made an object, you just have to set the name here. You don't have to do email, here only your name is coming, so how will you do it? You must have read dot dot dot spread operator. Dot dot dot data means the previous data will come here. If something is written in your email like something something something, then your email will come here something something something. And you will do change only in name. Comma name, this name is your key and in value you will come e dot target dot value. I will tell you more standard approach, first understand this a little bit. We will do it with more standardization. So, here you will get old data whether it is email or anything else. So, everything will come. Change will be in name only. You know the properties of the normal object. The value we are giving here is replaced by the old one. So, we have not given the old one. We have replaced the name only. The old one will remain and the name will be replaced. So, will it work? Let's do the CLG data here. And let's see. Let's reload. There is a problem here. Email is not defined. Remove the value field from here. Because the name will not be defined. We have an object here. And the name and email are not defined here either. So what we will do here. Data dot name. It is an object so we can access it like this. Data dot email. And the value that we removed, we can put it like this. But it is fine for now. So, what else is there? Email handler is not defined. Let's make an empty email handler. Because we have to make it further. Email handler. And here E is coming to us. And CLG. E dot target dot value. Let's do CLG now. Here our sit name handler will work now. And let's see what error is there. So many errors. Clear handler is also ours. We have not made clear handler. Let's make a clear handler too. Function. Clear handler. Now we will reload it. As soon as I write here, the name will be changed to email as it is. I have written M A N A S. The name is changed to email as it is in the object. We have used the spread operator and replaced the name. Now we can do the same with the email. Here you have set data. And here we have to set an object. If it is an object, then we will put an object here. So, we have to put an object. We have to keep the old data as it is. Like the name, phone number, etc. So, we have to keep the old data as it is. So, we have spread the old object here. You have got the old key value pairs here. What you have to replace is just the email. So, we will replace the email key with the e.target.value. Okay. Now reloading, I wrote name as it is. Now I will write email as it is. Clear is this much. If you clear, it is not clear because we have not made it clear. So what to do to clear? Nothing much. In the set data, empty the name as it is. Empty the email as it is. That's it. Clear is done. So we will clear it. But it didn't happen here. Because we haven't linked the value. So let's link the value. What will come in the value? Data.name. And what will come in the value here? Value data.email. And let's arrange it properly in the format. Something like this. Alright. Now, we have one more problem. We have made the state same but we are still making the handlers multiple. Is there any way to make the handlers same? Think carefully, is there any way to do the same for the handlers? And there was another syntax here, I was telling you about it. Here you can take the previous value, this is a function, you can see it in the functional update. So here instead of data, if you want to return the previous, then you have to wrap it in a parenthesis. You can do this functional update as well. This is the normal way of taking a function. In the function, you have the default parameter which is the previous value. The previous object. Whatever you have given here will be the previous value. If there is something in the name like an alpha or an empty email, then you will get the previous object. If you are getting the previous object, then what you have to do is to return it. Because you have used curly braces, so you have to return it. If you don't use curly braces, then I will show you both fatter and function. You will return it like this. What will you return? Object. What will you return in set data? Object definitely. So, dot dot dot. Previous object has come here. And if you want to change only in name, then name e dot target dot value. Like this. This will also work properly. See this. It is working. You are clearing it. It is clearing. Very good. You can do it this way. And if you want to use the fat arrow function, then this is even better. Here you have the previous. And in this way, here you can also use this normal syntax, then you will not have to return. If you put a parenthesis in this way, then you will not have to return. This is how your work will be done. These are all basic things. But mistakes happen definitely. So you can work in this way also. Now I was saying that let's put it in the old syntax. So I was saying something like this. Is there any way to make multiple handlers work in the same handler? Because We are doing the same logic wise. E.target.value is here. E.target.value is here. We are spreading the old data here as well. We are spreading the set data here as well. What is the change? This is a key change. So, if this is our name input field, then we can set the name of the key here. And if this is our email input field, then we can set the key of our email. Means, we can insert the key as a variable here. Is there anything like this? So, definitely you have this in the object. You can insert it as a variable here. Like, if I make a variable cons alpha and here I put name in this way. So, what is in alpha? This is name. Now, if I want to bring this alpha variable in this place, then how will I bring it? If I write alpha, then this becomes a key. Alpha becomes your key. If I write something here, then it is going in alpha. It is not going in name, it is going in alpha. But I need the value in alpha here. So, to insert it, I have to use the square bracket. In normal javascript, you have to use the square bracket. And your name will come in alpha. Now, I will reload. If I write here, then it is coming in the name. If I will do email instead of name, then it will go in the email. So, it is coming in the email. Why? Because your key has been emailed. So, is there any way by which I am typing, it will recognize that this is a name key, it will come in the name key, and if there is an email, then an email will come in the key place. Is there anything like this? Yes, there is something like this. Look, look, look, look, I will tell you now. I will tell you now. First normalize it, I will tell you now. I will tell you that you must have read that there is an attribute named name. You think that there is no use of it. Actually, there is use of it here. The attribute named name, you can use it here. As you were getting the e.target.value, you can also get the e.target.name. So, the name you will give here, set it in the key. If I put the name here, then this name will be set in your key. I put the email in the name here. So, you must be getting your email. Let me show you how. You can do CLG here. e.target.name. So, see now. I am typing here. See what I am getting. I am getting the name. If I do this in the email, e.target.name So, what is in the attribute of name? Email is in the attribute of name. This is the input field of email. Email is in the attribute of name. So, what is coming to you here? Email is coming. So, whatever you will give here in the name, you can receive it here. You can receive the value like e.target. Now, if this is received, then definitely you put it here. Your work will be done in single handler. How? I will tell you. See. function Make it simple here. Input handler. This will do all the work. Handler. Take E here. And now there is no need of these two. Here you have to pass the same input handler in both. Input handler here and input handler here. Now both will be handled by input handlers. How? I told you that you will get the name from e.target.name. And you will get the value from e.target.value. If you are writing alpha in name then it is writing in name. If you are writing in email then it is typing in email. I am getting to know from the name attribute. We have set the attribute here and we are getting it here. Now what will we do? We have to do set data. Everyone used to do that. So we did set data. Curly braces because we have to set the object. So, dot dot dot. We will spread the previous data here. Where will the previous data come from? This data. So, this is your previous data. Okay. Previous data has been spread. What do you have to replace? There will be only one at a time. Either you are typing in the name or you are typing in the input field of the email. So, how will it be known whether you are typing in the name or in the email? So, you will know that from e dot target dot name. Okay. So, now by destructuring here, remove both here. Which destructuring have you read? E.target is an object. In this, we are getting the name and the value. So, destructure the name and the value. This is done. We have destructured the name and the value in this way. Otherwise, we have to make different variables in this way. const name is equal to E.target.name. const email is equal to E.target.email. both are in target object so we have done destructure here name, email and here e.target that's it now we will use name as a variable as I told you we will use name as a variable because in key we should work like a key variable ok and here sorry I have put name email I had to put name and value ok name attribute and value attribute and put the value here ok name is here as a variable if name input field will be here then your name will be coming here if email input field will be here then your email will be coming here if phone number input field will be here then whatever you will set in name will be set here and your value Constantly, whatever you type here, it will keep coming. Okay? So, what we were doing with multiple handlers, we tied it to the same handler. Now, your same handler will also handle everywhere. Here also, we used the same handler. Here also, we used it. Whatever you make next, give the same handler. The work is over. Give the same handler. Increase the state here. That's it. Okay? Reload. I put name in minus. Here, name is coming. Put it in email. It is going to email. So, it is identifying whether it is typing in name or email. Now, if I want to add phone number here, it is very easy for me. I want to add phone number here. I have added phone number. Just pay attention to the key. The key you are giving here. So, just pay attention to the key. For example, you are changing the input here. So here you have to enter the same phone number in the name. Otherwise, if there is a difference in the key, then it will become a different key value in the object. So you have given the phone number here. The same input handler will remain. Rest, if you want to type a number, then you can type a number here. And data dot phone number definitely. OK. Reload it. So here you enter the email. Enter the name, enter the email and here you have the number. See this number. You have the number here. OK. Enter here in the placeholder. You can enter the phone number. And here you can type any number. See this is set in the number. Here you show the phone number in this way. That's it. So this way you can handle it very easily. And here is a tag and tag. So I hope you enjoyed it. Just keep this in mind. We have spread the old data. We had to take a new key value pair here. We need different key, we are typing in input, so we are typing in name input, so key should be name. We are typing in phone number input field, so key should be phone number. I thought something like this would happen here, like a variable, so we will use it here. So I thought name attribute is here, put name attribute in everything. Use it as a variable and put it here. And in value, definitely we were getting value from e.target.value. That's it. And do functional update like this, set data. and you will get the previous value. Write it in a short way. You don't have to do much here, return, etc. Put parentheses here because definitely you have to give the object here. Otherwise, you will have to return. So we have put parentheses here. There is no need to return. This is the object. Dot, dot, dot. You got the previous data. Give comma. Here you definitely have the name. And here, value. That's it. Your work is done. Your work is done. Did you clear it? It is clear. It is not here because we have not linked it here. Link it here. I have linked it with the data.phone number. I have linked it with the phone number. I am not clearing it. Clear it here. Phone number. Alright. Good. Reload. Name, email, dot com. See, I was putting anything in the email. Now you have the data, you can also validate it by putting reg x etc. We will see all that in the projects etc. And here I am typing something. I am clearing, everything will be clear. So you are able to work in a controlled manner. So it looks very easy to see that what is the input field, what is in it. But the strategies etc. that are there, production strategies etc. To understand all those things, It takes time, definitely. It is very easy to go by telling it easily. Definitely, I could have gone too. There was no problem. I would have told you a little about the input box. I would have done this, but it would not have been fun. Those who want to study basic, they do not like to study in depth. Let's skip it quickly. It was basic. A little bit of mind starts to wander ahead that we are going complex now. So now we will have to close the video. We will have to sleep a little. It happens like this. So, if all these things are happening with you, definitely, then do it with a little bit of effort. If you have read JavaScript properly, then it will be fun in React. It's just that one phase. HTML, CSS, JavaScript. Especially JavaScript. Those who have passed the JavaScript phase, then they find React easy. Then they find it easy to read Node.js in the backend. They can do Express.js easily. Then MongoDB is the database part. Everything is easy. There is a phase of JavaScript and logic. It is clear. And how is it clear? Definitely, while learning. You have seen the whole playlist on JavaScript. Keep practicing. How will you practice? The React I am teaching or giving tasks. You will practice with that. You can make JavaScript projects. You will practice with that. Continue. Don't stop. It's not like you have studied JavaScript and it's done. If you keep continuing, then logic will keep building. And then you will get clarity. Okay. So, that's it for now. See you in the next lecture. Till then, bye-bye.
