Podcast Lookup

Working with Radio Buttons in React.js – Handling User Selection and Form State

ReactJS Developers Podcast

  • December 3, 2026
  • 34 min
  • Episode 8
Episode page
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…
In this episode, we explore how to implement and manage Radio Buttons in React.js.Radio buttons are essential form elements that allow users to select one option from a group of predefined choices. They are commonly used in registration forms, surveys, questionnaires, settings pages, and other interactive web applications.You'll learn how to create radio button groups, manage selected values using React state, handle user interactions, and build controlled form components.Through practical examples, you'll understand how React handles form inputs and how to create more interactive and user-friendly applications.What You'll Learn
  • 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.

Who Should Watch?
  • Beginners learning React.js.

  • Frontend developers working with forms.

  • JavaScript programmers.

  • Software engineering students.

  • Developers building interactive registration and survey applications.

Transcript

From the publisher

This 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

00:00—
  1. Speaker 1

    Now in this part we are going to handle radio buttons. There is a lot of similarity between checkboxes and radio buttons. And there are some minor changes which confuse people a lot. So that's why I have already told you about checkboxes. Now we are discussing about radio buttons. So whatever confusion will be there in between, everything will be clear. And there are many scenarios. I had also told you about checkboxes. How to select all, how to deselect all, how to group. how to make a single function properly in the form, we are going to see in the radio button in the same way. And this is going on in the whole sequence, so see from the start, form handling, what things are there, then checkbox and then radio buttons. So it will be a lot of fun, you will get a proper understanding. Because I have discussed some things there, so I will not repeat it again. Like what is control component? There is a whole discussion on control component here. We will also work by linking radio buttons with state. Like we call it a controlled component. So, you can see all that. So, how to handle radio buttons? The main topic of this lecture is this. Okay. So, first of all, let's understand what is a radio button? How is it different from a checkbox? So, in a radio button, at a time, the pin is not connected, it's okay, I'll say it. At a time, we can select only one option. Means, in checkboxes, you have seen such round boxes. Multiple things you can check. Okay, you have a list in checkboxes. Multiple items you can check. But in radio buttons, what happens? At a time, you can select only one option. For example, you have male, female and others. Now, here, such an option should definitely not happen that someone is selecting male and female both. or female others, either it will be male or female or others. That is called radio buttons and you must have done it in HTML. So let's not discuss this much. So all radio buttons in the same group share the same name attribute. You must have read this in HTML and not done it. So now you will know how to group using the name attribute. There is a little difference here. Here we were using the name attribute in a different way. They will use it here too, but the name attribute is also used to group here. In react, we manage which one is selected using state. Let's see which one is selected using state. Let's save it in state. You might not have understood this state part. Let's make radio button and see. Everything will be clear. Let's make basic radio button. Let's understand how to work here. I have already installed Vite. I have also set up Tailwind CSS. Although I don't think that I will need much to set up. But if I need, then I will do it with Tailwind CSS. I have already taught Tailwind CSS. We will not use CSS much otherwise time will be wasted. So install Vite, set up Tailwind and come quickly. Even if you don't do Tailwind, there is no problem. You have to learn logic. You don't have to set up. So here input type radio, alright, and that's it, our radio button is ready. Really? Yes. But we haven't done the logic part yet, so our HTML radio is ready. Very good. I clicked on it, now if I click again, it won't untick, there is no checkbox. It will be selected only once, and it will remain selected until the other one selects it. This is how it works, let's see now. So this is our radio button, here we will put it in a label, right? All this will be clear in HTML itself. Here, let's give the label as mail. Alright. Now, it is not ticking when we click on mail because we have not connected the label. So, we will give the ID here. In ID and HTML4, we have to give the same value so that these two get connected. Alright. If I click on mail too, then our input box will be clicked. Something like this. Like, here, our mail is radio. So, here, mail itself It is not necessary that you have to give this. You can give anything. It should be the same in both. And the value should be the same in this. So both will be connected. Ok. So now if you see here. So it is connected. I clicked on it and it was selected. Alright. So this is our male. Very good. Now in the same way we will make it female. Here is our female. This is done. Female. Alright. And here also female. So now we are clicking on female. Female is also getting selected and male is also getting selected. This was not wrong. Either male will be selected or female will be selected. So we have to group it. To group it, we will use the name attribute here. Now the name you will give here, you have to give the same name here too. So both these inputs will be grouped together. Meaning, what is this? Male and female. Gender is there. So I will give gender in name here. As such, you can give any name, but I am giving gender here. So, I have given name gender here and gender here as well. So, now these two input boxes are denoting that I belong to the gender category. Okay, these two are grouped in gender. So, now either male or female can be in gender. So, now see here, it will work in this way. Either male or female. Both will never be selected together. Either male or female. and also give a line break in the middle so it will look good either male or female in the same way you make others here in the same way here you have others other what is other ok male female other here gender will remain type radio will remain and here other will be done so this is done but now here see it is doing proper work but now it is not linked to the state And it is not linked to the state. That means it is not a controlled component. It will be linked to the state. Then only we will call it a controlled component. Okay. So, linking to the state is our main purpose here. That how will we work with the state. Radio button and state. How will we link both of them. So, here first of all. Give a line break. It will look a little beautiful. Okay. Now let's see how to link with the state. So, it is doing our work properly. But we need value in the state. So, const gender comma setup. gender is equal to useState and here by default it will remain empty, nothing will remain in it. Okay? Okay. So now I want that as soon as I click on it, the gender will be set to male. So how will I do it? Definitely I will put onChange here. Only in text, here till now, onChange is running in all of them. Okay? So you have to put onChange. Changeable whatever things are there, like here in checkbox, your on or for was there, And if you are switching to radio button, then if you are changing or typing, then onChange is used in all of them. If you click, then also it will work, but onChange is fine here. Okay. So, what will we do in onChange? You will get an event object as always. We have discussed all this. So, here we will set gender. Now, what will we set in gender? So, we will get value from e.target.value. What value will we get? What value will we get here? We haven't set any value. So definitely when you are doing on change, you have to set the value here. Otherwise, you will not get any result. See, as I click on mail here. So did you get any result here? I haven't done console. I'll show you by doing console. Gender. Okay. I'll format it. And this on change. I'll put it here too. On change. I'll put it here too. Set gender. E.target.value. Okay. So, I clicked on male and it is on. Clicked on female and it is on. Clicked on other and it is on. So, the value is on here. As it was on in the checkbox, so how will we handle here? Here, you just have to give value like this. What do you have to set in value? If it is female, what do you want to show in value? If you want to show female, whatever value you give here, it will be in e.target.value. So, you have given female here. So, in e.target.value, female will be coming. And setGender will set female here. Okay. Just keep in mind that these two things are very important here. So, here we will give male. And coming down, here we will give gender in value. Sorry. Other. Okay. So, as soon as we click on male here, then e.target.value. What is the value? Male. So, here male will be set. In the same way, female will be set here and other will be set here. It is very important to give value otherwise, it will come on and off like this. So, we have got male, female, other and other. And it is coming twice, it is irritating. So, come here and switch off the strict mode again. Good. So, reloading like this, male, female, other. Okay. So, as soon as we are clicking here, it is changing in the state. But on changing in the state, our radio button is getting updated. This should also happen, right? That's why we will say that it has properly synced with the state. Right? When the radio button is changed, it is changing in the state. And when the state is changed, the radio button should also change. That's why we can properly say that it has synced. So here, when the radio button is changed, it is changing in the state. But if I change it to female in the state, then will it be female by default? Come on. If I change it to female, then will it be female by default? No. If I change it to other here, So, will it be other by default? No. That means, our state is still not linked properly with the radio button. So, how will we do it? So, here also you have, like there was an attribute named checked in the checkbox, here also you have checked. Okay. So, if you give true in checked, then this will be true. If you come here at the bottom and give true in checked, then will this also be true? No. here only one of the two will be true at once. If this is true, the one below, then the one above will be overrided. Okay? Then after that, if I give it here in the middle, checked, true in female, then here again, the other one is the same. Because first, the male one was running the code, then the female one, then the other one, then the other one overrided everything, here the other is selected. So, in this way, you can make checked true or false. So, what we have to do here? How will we check which one should be ticked, which one should not be ticked, which one should be checked, which one should not be checked? So, definitely, we will put a condition here. What kind of condition? So, condition will be very simple here. You have to understand carefully. You will understand it well. See, what I am doing. I am writing here, if our gender is is equal to male. I have put the same value here. So, I am checking if our gender is set. If it is equal to male, then this input box will be checked. It is a very simple condition. If gender is equal to male, then this input box will be checked. And if it is equal to female, is equal to female, then this entire input box will be checked. And if our other is equal to, one minute, here if our, copy it properly, if our other is equal to, then our other will be checked. That means, in one go, only one is happening. Okay, so in this way, we can properly link the state here. So now, by default, what have we given? Other. So here, see, this other one is selected. We will do female. What went in state? Female went. So, he checked that where is gender female? Where is it equaling? So, it is equaling here. So, it is completely checked. Okay. Something like this is working here. So, in this way, we linked state with radio button. And when click on radio button, change happens. Change happens in state. We made it like this. So, in proper way, it will work in sync now. Okay. Properly, it will now It will work in sync. And similarly, there is nothing complicated in radio button. Like in checkbox, we discussed multiple things like select all, deselect all, group item, something, something, something. There is not much in radio button because it is simple. You have to select only one at a time. So, you are selecting only one at a time. That's it. Now, the method of grouping here is a little bit. See, group has actually been done. But here, the code is getting a little bit lengthy. Process is similar in all. See, on change, everyone is doing the same work. Here also, on change, everyone is doing the same work. Here also, everyone is doing the same work. then this checked one, then all these are almost the same, so why not map it here, why not map it here, okay, although if you make a function, then if you make a function, then you will pass the same function here, but again, the code is lengthy, right, the rest of the work is also the same, so why not map it, it will be much better, So, we can work by grouping them like this. So, this was the basic radio button that you understood. There is no issue if you do it like this. But to make the code a little smaller, to make it more readable, we do it like this. Let's group the radio buttons using map. Okay. Let's make a map like this. Whatever things you want to list, male, female, and other, whatever. So, let's do it like this. So, if you want its code, I will give it to you here. Again. Inside notes, we have this. Basic.jsx. Okay. This will be in notes. Now I will show you how to write a group. We didn't need to write such a long code. What did I want? Male, female, other. I will make an array here. Gender. Gender. Genders. Okay. I will make an array here. Male. Male. Female. And other. This is our array. Now we will map this array. That's it. Gender.map. You already know how to map. Map. There will be an item coming here. And we will return it like this. It is important to return. Why? Because we have used the syntax of curly braces here. So we will work with this one label. And the rest of them. Wait a minute. We will put this whole input box here. And this will work. There is no need for all these things now. Same thing will work in ID too. Type will be ready. Name and gender will be in all. So that we can group. Right? We can find out that these three are of same category. So here name and gender. What will come in value? So in value the same item will come. Okay. Male is male. Female is female. It is being listed one by one. Basically it is being mapped. So male is male. Female is female. This is done. Okay. Now let's talk about checked and on change. So on change here. Nothing will be different. Here E. and we will set gender as e.target.value. And where is the value coming from? The value of the one who will click on it will be saved here. Okay? So, as the female will click on it, the female will come. If the male will click on it, the male will come. I will show you the gender by doing CLG. Look at this. Let's reload. And something has been issued here. What has been issued? e.target.value And gender. Okay, we have mapped the gender here. We had to map the genders. Gender is the area. Alright. So, male. Why is it coming in male? Change has actually happened. We have just hardcoded it here. So, an item will also come here. So, male, female, other. So, other is written on other. Female is written on female. Male is written on male. And it is saying to give a unique key too. To identify uniquely. So, we have given the key too. Very good. Other, female, male. Female, other. Very good. It is working properly. But, but, but, but, there is still a problem here. When the state changes, it will not change here. Because we have not used the checked attribute yet. Have you used the checked attribute? Let's reload. By default, there is other here. So, it should have been ticked on other. But, it is not. So, state also has to be linked here using checked attribute. So, what will we do in checked? Same process, same. If our gender is equal to item. Basically, if we click on female. So, what did you get in gender? Female. Now, here are three items. Male, female, other. So, whoever's gender is female, only that one will be ticked. That's it. This is a small concept here. We just mapped it. So, it was submitted in one line. And key is not here. Key is given to parent. Main parent is here. Okay? Great. So, now there will be no error here either. Other, female, male. This is working properly. So, we can group in this way. And this is a more proper way to handle radio buttons. Okay? Mostly, we do it in this way. Rest, you will generally see in projects in that way too. But, it is very lengthy. If, let's say, 6, 7 are done. If there was language here, If I had given three labels and three input boxes, I wouldn't have been able to change it easily. So here, HTML, CSS, JS, a lot of them. React, then you would have some JavaScript here. JavaScript is done. After React, our next.js would come and then we would have MongoDB, AWS, SQL, Docker. If we had so many things here, it would be so difficult to do them one by one. But now it is easy. MongoDB, AWS are doing proper work. It is doing proper work. There is no problem here. So this is the way we have to work in production. So now you know how to group radio buttons. Let's move on. Here you can see the summary. Here you can see the name, examples, and concepts to be grouped. Concept, description, example, and this concept has been repeated. This column has been repeated. I will remove it from the notes. So here you have the name, description, and example. Here, you have the option that defines. The value you will give is basically the same option. If you are giving mail in value, then one option is your mail. Because you will get the value by clicking on mail. Then check determines which one is selected. You can update the state with onChain. And we will use useState to control the selection. That's it. Now, here is a question and it is a very nice question. You will make it yourself. And I will definitely tell you. But first of all, you will make it yourself. Then you will see the solution that I have made it properly or I have made a mistake or I have made it in a different way. Mine is better or theirs is better. You will compare. So quickly pause and make it. And now I am telling you how it will be made. So imagine you have a list of payment options. You have a list of payment options and they have given a list. And want to display the details for the selected one. That means here you have to create radio buttons. Payment option. See, they have clearly said in payment option. Imagine you have a list of payment options. So, you will be able to select only one payment option at a time. If you select two payment options, then you will never make a payment. So, only one payment option will be selected at a time. So, we have to select. That means we have to use radio buttons. Okay. And want to display detail for the selected one. And the selected one, related to that detail, related to that info is given here. That you have to show on the screen. Just now, we don't have to do payment. Just now, we have to show. First of all, we have to make a radio button by listing it. So let's make it quickly. I'll give it to you in notes. This was our grouped radio.jsx. Very good. So the group is already done. In this way, you can show it here by starting. Let me show it by starting. remove this remove this and what is it? it was payments right? payments so in payments there is one object ID is info in ID UPI card cash on delivery UPI card and cash on delivery alright we have ID UPI UPI then there is some info blah blah blah blah and then after that replicate it in the same way UPI card and here we will get COD cash on delivery and instant transfer via UPI apps. We will have to write instant transfer via UPI apps. What happens in card? Pay using credit or debit card. This is true. Pay using credit or debit card. In cash on delivery, basically we pay when the order arrives. Pay when the order arrives. Pay when the order arrives. So, first of all, you have to list it and show the radio button. This is the first thing. Simple thing. You have to show the radio button by listing it. How will you do it? So, here, you have to use JavaScript. I have closed curly braces. Payments.map You will get each object here. This item will be each object. Because there is each object in the array. Okay. Don't forget to return. Otherwise, you won't see anything. So, here, first of all, label. And in the label you have input. And type will be radio. Alright. And with label. To link it. First of all let's give item.id here. So that it is visible. UPI card and cash on delivery. So here to link it with label. We will put html4 and id. So in html4 and id. We will put our item only. In id also. Here you will get item dynamically. So that's all we have. And until we don't give the name, this group will not happen. So it is important to give the name. What should I give the name? Let's just give the payment. Okay. So let's reload here. Look at this. So it's not working. Why? If you give the key, then the item will go to error. We have given the name as payment. Okay. Very good. So this should work by default. card, cash on delivery, okay, clicking on this is not working, clicking on this is working, okay, and first of all, select none, it is getting selected, class name, select none, okay, so now I will click on it as much as I can, sometimes suggestions are not found, so now I will try as much as I can to select it, a green line is not coming, it is selected, the green line that was coming, If I click on this, then it is also happening. If I click on this, it is also happening. If I click on this, then it is also happening. We have given something wrong. ID is an item, HTML4 is also an item. Okay, we didn't have to give the item. The item is the whole object. We had to give item.id. Okay, item.id. You can also give item.id in the key. This item is an entire object. We have to give an ID, right? UPI, card and cash on delivery. So, HTML for UPI. ID is also UPI. So, both are UPI. So, click on UPI. Click on card. And then it repeats again. I don't know how. I don't know how. How does anything happen? Wait. Let me see what's happening. Okay. I didn't reload. So, COD card UPI is working properly. COD card UPI. Okay. COD card UPI. Now, first of all, we have to save it in the state. Whichever is the selected mode. So, const selected mode of payment comma set selected mode is equal to use state. And by default, it will remain empty. Alright. So, put on change here. Quickly. On change. E and here set selected mode. E.Target.Value. Now value will not come here, it will come on and off. So for that you have to pass the value item.id. So wherever I click, its item.id. Means if I click on UPI, then UPI. If I click on card, then card. If I click on COD, then COD will be available here in E.Target.Value. And here it will also be set. So after being set, let's console and see. Proper set. is happening or not. Reload it. Cash on delivery set, card set, UPI set. Very good. Wow. Wow. So, this is what we had to do. And what else? Okay, we had to show info. We had to show info related to what would have been selected. Okay. So, let's make a paragraph here. Make it outside the map. Otherwise, it will be repeated. So, here, mode of payment will be selected mode. Ok. And here we have to show the details related to it. Details. So now how will we get the details? Tell me how will we get the details? We have given selected mode of payment here. How will we get the details? Tell me. So we are selecting. So the mode of payment is coming. We have to find the details related to it. How will we find the details now? How will we find the details now? So there are two ways. Either set the whole object. Set the entire object in the selected mode. Okay. And one more thing, we haven't given the checked attribute here. In checked, we have to give it here, right? That's why it will be linked. Otherwise, let's assume I have given by default here mode UPI. So this UPI will not be selected if I don't give checked. See this. No. So here we have to give checked too so that it will be linked properly with state. So I will give it in checked. Selected mode Whichever item will be equal to ID, it will be selected. Okay. Cards, UDI, UPI. Very good. And by default, what will be UPI? Because I have given UPI. Alright. So, here we have to show details also. So, for this, 2-3 things can be done. Either you save the whole object inside this. So, you will get info automatically. Or you can find. How will you find? So, to find, you have to read this info. Payment, which is your array. If you want to find something in an array on the basis of a condition, then we use the find method. This is definitely a higher order function, so it is taking a function inside itself. It is similar to a map. You are getting each item. According to that item, you put your condition and if the condition matches, then that item is returning you. The whole array is returning in the map. Here you are returning the array. Exactly, the array is returning in the map. In forEach, you don't have any return. Here you have single, that item is being returned which matches the condition. For example, here you will have one object. Leave the item, I will call it object. One object will be coming. If object.id becomes triple equal to UPI, then here the entire UPI object will be returned. See. the entire object of UPI will be returned. If I write COD here, COD, then because this condition is matching with COD, then the entire object of COD will be returned. Okay? So, this is how our work is done. So, here, filter, etc. There can be more ways according to you. You can see how you will do it. Here, the find one is better because we have to remove only one from here. If you want, you can also do filter. Filter... But you will be getting an array there. So, there you will get curly braces 0 and dot notation. So, it will be a little long. There are many methods, but you have to find the proper method. So, I am looking for this proper method here. So, you can see payments.find in this way. And you have got the whole object. So, we can see info by doing object.info. So, you can see info. Whatever will be selected, it will be the info. COD info came. Card info came. Did the card come? No. Did the UPI come? No. Okay. Here we have fixed the cash on delivery. Copy it from here. Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? Did you get the info about the card? It will find that item and give you. Basically, it will find that object and give you. And we have to see the info of that object. So, we will get the info from here. So, we are getting the info of COD. We are getting the info of CARD. We are getting the info of UPI. Okay. You could have done it this way. Or you could have saved the whole object here. Instead of saving the value. Instead of saving the value. You could have put the whole object if you want. The object that you are getting. So here we can put the whole object, but I don't think this is the proper way. The whole object will be saved in it. In selected mode, the whole object will come to you. So by default, this is an empty object. And here you will have, as soon as you change on, the whole object will go in it. Okay, selected mode. See where you have used selected mode. So selected mode dot, ID you have to write here so that you can show ID. And here you will get the details directly, there is no need to do so much. In this way, you will get directly from selectedMode.info. And selectedMode.id is equal to item.id. And everything else is fine. If you want, you can come here and see, reload. So, we clicked on COD and COD is selected. We can see COD ID and details related to it. We can see card ID and details related to it. We can see UPI ID and details related to it. So, you have multiple options like this. You can think logically about the possibilities. What are the possibilities and how can you do it? What is proper? You have to think about it. Right? So, there was a way to do it in the same way as find. This one was more simple, but you saved the whole object in the mode of payment. If you want to see the ID, you can see the ID. If you want to see the info, you can see the info. That's it. Okay. So, take both the methods. Take both the methods. I will copy it. I will copy it and paste it down here. Wait a minute. method1. jsx method1. jsx method1. jsx method1. jsx method2. jsx method2. jsx method2. jsx method2. jsx method2. jsx This is also working properly. Yes, it is working properly. Okay. This is your method2. method2. jsx So, you can see how to do it, what to do. And in method1, here, if you want to put something in selected mode by default, then this time you will have to put the whole object. Okay. If you want to put some selected mode in default, then this whole, You will have to enter the object. It will still be selected. So I hope you understood. And you must have enjoyed it. Okay. You must have understood. You must have enjoyed it. I will push the code. And you will get notes. Don't worry too much. And videos are coming regularly. So it will look good. And it will look bad from before. Because it is already so late. Okay, so now practice this much. Then we will meet in the next lecture. There we will see a select drop-down, how to handle it. And then we will move forward. We have to see a lot of things. So quickly do it. Okay, do the tasks that I will give you. Okay, so thank you so much. And then we will meet in the next lecture. In the next part, till then, keep exploring and keep reading. Bye-bye.