← Back to Chapters

Checkbox and Radio Buttons in React

☑️ Checkbox and Radio Buttons in React

⚡ Quick Overview

React handles checkbox and radio inputs as controlled components, just like text fields. Instead of using the value property to reflect the current selection, these inputs mainly rely on the checked attribute plus onChange handlers that update React state.

By binding the UI to state, your checkboxes and radio buttons always stay in sync with the underlying data, making complex forms predictable and easier to debug.

? Key Concepts

  • Checkboxes use the checked attribute instead of value.
  • Radio buttons are grouped by the name attribute and allow only one selection per group.
  • Use arrays in state to manage multiple checkbox selections (e.g., hobbies or skills).
  • Always handle user input using onChange and update React state accordingly.
  • State is the single source of truth; the UI simply mirrors it.

? Syntax & Theory

A checkbox in React is controlled by mapping a boolean state to the checked prop and updating that state inside an onChange handler:

Radio buttons work similarly but represent a single choice from a fixed set. All radio buttons in a group share the same name and compare state against their individual value.

For multiple checkboxes (multi-select), we typically use an array in state and push / filter values in response to user actions.

? Single Checkbox (Controlled)

A single checkbox can be controlled using a boolean state variable that tracks whether the option is enabled or disabled.

? View Code Example
// Single checkbox with controlled React state
import React, { useState } from "react";
function SingleCheckbox() {
const [isSubscribed, setIsSubscribed] = useState(false);
const handleChange = (e) => setIsSubscribed(e.target.checked);
return (
<div>
<h4>Single Checkbox Example</h4>
<label>
<input
type="checkbox"
checked={isSubscribed}
onChange={handleChange}
/>
Subscribe to Newsletter
</label>
<p className="mt-2">
Status: {isSubscribed ? "Subscribed ✅" : "Not Subscribed ❌"}
</p>
</div>
);
}
export default SingleCheckbox;

? Live Output / Explanation

When you tick the checkbox, the isSubscribed state becomes true, and the text updates to “Subscribed ✅”. When you untick it, the state becomes false and the message changes to “Not Subscribed ❌”. The UI always mirrors the current boolean value.

? Multiple Checkboxes (Array State)

When you have several checkboxes (for example, selecting hobbies), it’s convenient to store the selected values inside an array and add or remove entries as the user clicks.

? View Code Example
// Manage multiple checkbox selections using an array
import React from "react";
function MultiCheckbox() {
const [hobbies, setHobbies] = React.useState([]);
const handleChange = (e) => {
const { value, checked } = e.target;
if (checked) {
setHobbies([...hobbies, value]);
} else {
setHobbies(hobbies.filter((hobby) => hobby !== value));
}
};
return (
<div>
<h4>Select Your Hobbies</h4>
<label className="me-3">
<input type="checkbox" value="Reading" onChange={handleChange} /> Reading
</label>
<label className="me-3">
<input type="checkbox" value="Music" onChange={handleChange} /> Music
</label>
<label className="me-3">
<input type="checkbox" value="Traveling" onChange={handleChange} /> Traveling
</label>
<p className="mt-3">
Selected: {hobbies.length > 0 ? hobbies.join(", ") : "None"}
</p>
</div>
);
}
export default MultiCheckbox;

? Live Output / Explanation

Each time you tick a hobby, its label (e.g., "Music") is added to the hobbies array. Unticking removes it. The paragraph at the bottom prints hobbies.join(", "), listing all currently selected hobbies or “None” when the array is empty.

? Radio Buttons (Single Selection)

Radio buttons let users pick exactly one option from a group. All inputs in the group share the same name, but each has a distinct value. The checked one is determined by comparing state with that value.

? View Code Example
// Radio button group controlled by a single state value
import React from "react";
function GenderSelector() {
const [gender, setGender] = React.useState("");
const handleChange = (e) => setGender(e.target.value);
return (
<div>
<h4>Select Gender</h4>
<label className="me-3">
<input
type="radio"
name="gender"
value="Male"
checked={gender === "Male"}
onChange={handleChange}
/>
Male
</label>
<label className="me-3">
<input
type="radio"
name="gender"
value="Female"
checked={gender === "Female"}
onChange={handleChange}
/>
Female
</label>
<label>
<input
type="radio"
name="gender"
value="Other"
checked={gender === "Other"}
onChange={handleChange}
/>
Other
</label>
<p className="mt-2">
Selected Gender: {gender || "None"}
</p>
</div>
);
}
export default GenderSelector;

? Live Output / Explanation

Only one radio option can be selected at any time because all inputs share the same name="gender". Clicking an option sets the gender state to that option’s value, and the paragraph displays the currently selected gender or “None” if nothing is chosen.

⚙️ Combined Preferences Form

You can combine checkboxes and radio buttons into a single form with a shared state object. This scales well as forms grow more complex.

? View Code Example
// Full form combining radio buttons and multiple checkboxes
import React from "react";
function PreferencesForm() {
const [formData, setFormData] = React.useState({
gender: "",
notifications: false,
interests: [],
});
const handleCheckbox = (e) => {
const { value, checked } = e.target;
setFormData((prev) => ({
...prev,
interests: checked
? [...prev.interests, value]
: prev.interests.filter((v) => v !== value),
}));
};
const handleRadio = (e) => {
setFormData({ ...formData, gender: e.target.value });
};
const handleToggle = (e) => {
setFormData({ ...formData, notifications: e.target.checked });
};
const handleSubmit = (e) => {
e.preventDefault();
alert(JSON.stringify(formData, null, 2));
};
return (
<form onSubmit={handleSubmit}>
<h4>User Preferences</h4>
<div className="mb-3">
<strong>Gender:</strong><br />
<label className="me-3">
<input
type="radio"
name="gender"
value="Male"
checked={formData.gender === "Male"}
onChange={handleRadio}
/>
Male
</label>
<label className="me-3">
<input
type="radio"
name="gender"
value="Female"
checked={formData.gender === "Female"}
onChange={handleRadio}
/>
Female
</label>
</div>
<div className="mb-3">
<strong>Interests:</strong><br />
<label className="me-3">
<input type="checkbox" value="Coding" onChange={handleCheckbox} /> Coding
</label>
<label className="me-3">
<input type="checkbox" value="Music" onChange={handleCheckbox} /> Music
</label>
<label className="me-3">
<input type="checkbox" value="Sports" onChange={handleCheckbox} /> Sports
</label>
</div>
<div className="mb-3">
<label>
<input
type="checkbox"
checked={formData.notifications}
onChange={handleToggle}
/>
Receive Email Notifications
</label>
</div>
<button className="btn btn-primary">Save Preferences</button>
</form>
);
}
export default PreferencesForm;

? Live Output / Explanation

This form captures gender (radio), a list of interests (multiple checkboxes), and a notification toggle (single checkbox). On submit, the complete formData object is shown via alert(JSON.stringify(...)), making it easy to see exactly what would be sent to your backend.

? Key Takeaways

  • Use checked (not value) to bind checkbox and radio state.
  • Group radio buttons with the same name so only one is selectable.
  • Use arrays to store multiple checkbox selections (add on check, remove on uncheck).
  • Keep all form state in React and update it through onChange handlers.
  • Think of state as the “source of truth” and the DOM as a reflection of that state.

? Common Use Cases

  • Checkboxes for newsletter opt-in, terms acceptance, and feature toggles.
  • Multiple checkboxes for skills, interests, filters, and multi-select tags.
  • Radio buttons for payment method, gender, plan selection, and shipping options.
  • Combined preference forms where several input types contribute to one state object.

? Tips & Best Practices

  • Store multi-select checkbox values as an array of clear, descriptive strings.
  • Use meaningful value attributes like "coding" instead of "option1" for easier processing later.
  • Ensure all radio options in a logical group share the same name for both behavior and accessibility.
  • Always provide visible labels for inputs so users know what each checkbox or radio button means.
  • Consider extracting reusable checkbox / radio components in larger forms to reduce duplication.

? Try It Yourself

  1. Create a survey form with gender (radio) and hobbies (checkboxes).
  2. Display selected options in real time below the form using JSON.stringify or formatted text.
  3. Add a “Select All” checkbox that checks/unchecks all hobbies at once.
  4. Experiment with saving and restoring checkbox state from localStorage.

Goal: Learn to manage checkbox and radio input states in React using controlled components, ensuring accurate and synchronized user selections across your entire form.