Skip to content
the poor coder

how to use radio buttons in react class-based components

Poor Man
Published
Reading time
2 min read
On this page

Using Radio Buttons in React Class-Based Components#

If you want to create a form with radio buttons in a React class-based component, there are a few steps you need to follow.

Step 1: Import React and Component#

JavaScript

import React, { Component } from 'react';

Step 2: Create a Class-Based Component#

Create a class-based component by extending the Component class.

JavaScript

class MyForm extends Component {
  render() {
    return (
      <div>
        {/* radio buttons will go here */}
      </div>
    );
  }
}

Step 3: Create State for Radio Buttons#

Create state for the radio buttons in the constructor method of the component.

JavaScript

class MyForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedOption: ''
    };
  }

  render() {
    return (
      <div>
        {/* radio buttons will go here */}
      </div>
    );
  }
}

In this example, we're setting the initial state of the selectedOption property to an empty string.

Step 4: Create Radio Buttons#

Create radio buttons inside the render method of the component.

JavaScript

class MyForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedOption: ''
    };
  }

  render() {
    return (
      <div>
        <label>
          <input
            type="radio"
            value="option1"
            checked={this.state.selectedOption === 'option1'}
            onChange={e => this.setState({ selectedOption: e.target.value })}
          />
          Option 1
        </label>
        <label>
          <input
            type="radio"
            value="option2"
            checked={this.state.selectedOption === 'option2'}
            onChange={e => this.setState({ selectedOption: e.target.value })}
          />
          Option 2
        </label>
      </div>
    );
  }
}

In this example, we're using the checked property to determine if a radio button is selected or not. We're also using the onChange event to update the selectedOption state when a radio button is selected.

Step 5: Add Submit Button#

Add a submit button to the form.

JavaScript

class MyForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedOption: ''
    };
  }

  handleSubmit = event => {
    event.preventDefault();
    console.log(this.state.selectedOption);
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          <label>
            <input
              type="radio"
              value="option1"
              checked={this.state.selectedOption === 'option1'}
              onChange={e => this.setState({ selectedOption: e.target.value })}
            />
            Option 1
          </label>
          <label>
            <input
              type="radio"
              value="option2"
              checked={this.state.selectedOption === 'option2'}
              onChange={e => this.setState({ selectedOption: e.target.value })}
            />
            Option 2
          </label>
        </div>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

In this example, we're adding a handleSubmit method that will be called when the form is submitted. We're also wrapping our radio buttons in a form element and adding a submit button.

That's it! Now you know how to use radio buttons in a React class-based component.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC