Category: react
radio button value in React.
Published on 17 Apr 2026
Explanation
Import useState to store the selected radio
button value in React.
Code:
import React, { useState } from "react";
Explanation
Create a state variable to track the
selected radio option.
Code:
function RadioExample() {
const [selectedValue, setSelectedValue]
= useState("");
Explanation
Update state when a radio button is
selected using onChange event.
Code:
<input
type="radio"
name="gender"
value="Male"
onChange={(e) =>
setSelectedValue(e.target.value)}
/> Male
<input
type="radio"
name="gender"
value="Female"
onChange={(e) =>
setSelectedValue(e.target.value)}
/> Female
Explanation
Display the selected radio button
value dynamically.
Code:
<p>Selected Value: {selectedValue}</p>
Explanation
Export the component so it can be
reused in other files.
Code:
export default RadioExample;