Author -  Sai gowtham

How to use Radio Buttons in React

In this tutorial, we are going to learn about how to use radio buttons in react with class-based components and hooks.

Radio buttons

Radio buttons are used to select exactly one option from the available list, for example, choose the correct answers.

Using Radio buttons in React

To use the radio buttons in react we need to group the radio buttons with a name attribute and onChange event handler method is added to each radio button to access the data.

Here is an example:

import React,{Component} from 'react';

class App extends Component {
    state = {
      gender:""    };

    handleChange=(e)=>{
        this.setState({
          gender: e.target.value        })
    }

  render() {
    return (
      <div>
         <form>
            <input type="radio" value="male" id="male"
              onChange={this.handleChange} name="gender" />            <label for="male">Male</label>

            <input type="radio" value="female" id="female"
              onChange={this.handleChange} name="gender"/>            <label for="female">Female</label>
         </form>

         <p>You gender is --> {this.state.gender}</p>      </div>
    );
  }
}

In the above code, we have grouped two radio buttons with a name attribute value to gender.

When a user selects the radio button handleChange() method is invoked and access the radio button value to update the state.

Output:

using radio buttons react

Using Radio buttons in React Hooks

Let’s see how to use the radio button in hooks.

import React, { useState } from 'react';
function App() {
   const [gender,setGender]=useState('');
   const handleChange=(e)=>{
       setGender( e.target.value);    }

    return (
      <div>
         <form>
             <input type="radio" value="male" id="male"
               onChange={handleChange} name="gender" />
             <label for="male">Male</label>

            <input type="radio" value="female" id="female"
              onChange={handleChange} name="gender"/>
            <label for="female">Female</label>
         </form>

         <p>You gender is --> {gender}</p>
      </div>
    );
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY