Open In App

ReactJS UI Ant Design Radio Component

Last Updated : 21 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Ant Design Library has this component pre-built, and it is very easy to integrate as well. Radio Component allows the user to select one option from a set. We can use the following approach in ReactJS to use the Ant Design Radio Component.

Radio Methods:

  • blur(): This method is used to remove the focus from the element.
  • focus(): This method is used to get the focus on the element.

Radio Props:

  • autoFocus: It is used to get focus when the component is mounted.
  • checked: It indicates whether the radio is selected or not.
  • defaultChecked: It is used to specify the initial state of whether the radio is selected or not.
  • disabled: It is used to disable the radio.
  • value: It is used to denote the value for the radio button which is selected.

RadioGroup Props:

  • buttonStyle: It is used to denote the style type for the radio button.
  • defaultValue: It is used to define the default selected value for the radio button.
  • disabled: It is used to disable all radio buttons.
  • name: It is used to define the name property of all input children which are of type radio.
  • options: It is used to set children optional.
  • optionType: It is used to specify the option type for the radio.
  • size: It is used to denote the size of the radio.
  • value: It is used for setting the currently selected value.
  • onChange: It is the callback function which is triggered when the state changes.

Creating React Application And Installing Module:

  • Step 1: Create a React application using the following command:

    npx create-react-app foldername
  • Step 2: After creating your project folder i.e. foldername, move to it using the following command:

    cd foldername
  • Step 3: After creating the ReactJS application, Install the required module using the following command:

    npm install antd

Project Structure: It will look like the following.

Project Structure

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

App.js




import React, { useState } from 'react'
import "antd/dist/antd.css";
import { Radio } from 'antd';
  
export default function App() {
  
  const [currentValue, setCurrentValue] = useState("Goa")
  
  return (
    <div style={{ display: 'block', width: 700, padding: 30 }}>
      <h4>ReactJS Ant-Design Radio Component</h4>  
      <Radio.Group onChange={(e) => {
        setCurrentValue(e.target.value)
      }} value={currentValue}>
        <Radio value={"Goa"}>Goa</Radio>
        <Radio value={"MP"}>MP</Radio>
        <Radio value={"Delhi"}>Delhi</Radio>
        <Radio value={"UP"}>UP</Radio>
      </Radio.Group> <br />
      Current Selected Option: {currentValue}
    </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://ant.design/components/radio/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads