ReactJS radio button – Code Example

Total
0
Shares

In this article we will provide you code for ReactJs radio buttons. It will help you in defining radio button in JSX, group them, access onChange() event and take actions.

Code Example –

import React from "react";

export default function App() {
  const [radioVal, setRadioVal] = React.useState();

  return (
    <div>
      <form>

        <input
          type="radio"
          name="group1"
          value="ironman"
          checked={radioVal === "ironman" ? "checked" : ""}
          onChange={() => setRadioVal("ironman")}
        />{" "}
        Ironman <br />

        <input
          type="radio"
          name="group1"
          value="captain"
          checked={radioVal === "captain" ? "checked" : ""}
          onChange={() => setRadioVal("captain")}
        />{" "}
        Captain America
        <br />

      </form>

      <p>Selected Value: {radioVal}</p>
    </div>
  );
}

The output will be –

reactjs radio button