Hackforge Academy

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;

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community