Checkboxes and radio buttons let users pick options. Handling them means your app knows what the user chose.
Handling checkboxes and radio buttons in React
const [value, setValue] = useState(false); // For checkbox <input type="checkbox" checked={value} onChange={e => setValue(e.target.checked)} /> // For radio button const [selected, setSelected] = useState('option1'); <input type="radio" name="options" value="option1" checked={selected === 'option1'} onChange={e => setSelected(e.target.value)} />
Use checked to control if the box or button is selected.
Use onChange to update state when user clicks.
const [isChecked, setIsChecked] = useState(false); <input type="checkbox" checked={isChecked} onChange={e => setIsChecked(e.target.checked)} />
const [selectedOption, setSelectedOption] = useState('option1'); <input type="radio" name="options" value="option1" checked={selectedOption === 'option1'} onChange={e => setSelectedOption(e.target.value)} /> Option 1 <input type="radio" name="options" value="option2" checked={selectedOption === 'option2'} onChange={e => setSelectedOption(e.target.value)} /> Option 2
const [checkedItems, setCheckedItems] = useState({ item1: false, item2: false }); <input type="checkbox" checked={checkedItems.item1} onChange={e => setCheckedItems(prev => ({ ...prev, item1: e.target.checked }))} /> Item 1 <input type="checkbox" checked={checkedItems.item2} onChange={e => setCheckedItems(prev => ({ ...prev, item2: e.target.checked }))} /> Item 2
This React component lets users pick if they want a newsletter and choose a favorite color. It updates the page instantly to show their choices. It uses accessible labels and live region for screen readers.
import React, { useState } from 'react'; export default function Preferences() { const [newsletter, setNewsletter] = useState(false); const [color, setColor] = useState('red'); return ( <main> <h1>Choose your preferences</h1> <label> <input type="checkbox" checked={newsletter} onChange={e => setNewsletter(e.target.checked)} aria-checked={newsletter} aria-label="Subscribe to newsletter" /> Subscribe to newsletter </label> <fieldset> <legend>Pick your favorite color</legend> <label> <input type="radio" name="color" value="red" checked={color === 'red'} onChange={e => setColor(e.target.value)} aria-checked={color === 'red'} /> Red </label> <label> <input type="radio" name="color" value="blue" checked={color === 'blue'} onChange={e => setColor(e.target.value)} aria-checked={color === 'blue'} /> Blue </label> </fieldset> <section aria-live="polite"> <p>Newsletter subscription: {newsletter ? 'Yes' : 'No'}</p> <p>Favorite color: {color}</p> </section> </main> ); }
Always use checked and onChange together to control inputs.
Use name attribute for radio buttons to group them properly.
Adding aria-checked and labels improves accessibility for screen readers.
Checkboxes let users pick multiple options; radio buttons let them pick one.
Use React state to track if boxes or buttons are selected.
Update state on onChange to keep UI and data in sync.
Practice
Solution
Step 1: Understand checkbox behavior
Checkboxes let users select any number of options, including none, one, or many.Step 2: Understand radio button behavior
Radio buttons restrict selection to exactly one option within a group.Final Answer:
Checkboxes allow multiple selections; radio buttons allow only one selection. -> Option DQuick Check:
Checkboxes = multiple, Radio buttons = single [OK]
- Confusing which input allows multiple selections
- Thinking radio buttons allow multiple selections
- Assuming checkboxes allow only one selection
Solution
Step 1: Use checked attribute for checkbox state
Checkbox usescheckedprop to reflect its state, notvalue.Step 2: Use onChange event and e.target.checked
TheonChangeevent providese.target.checkedboolean to update state.Final Answer:
<input type='checkbox' checked={checked} onChange={e => setChecked(e.target.checked)} /> -> Option BQuick Check:
Checkbox state = checked + onChange with e.target.checked [OK]
- Using e.target.value instead of e.target.checked
- Using onClick instead of onChange for checkbox
- Binding value instead of checked attribute
function RadioGroup() {
const [selected, setSelected] = React.useState('');
return (
<div>
<input type='radio' name='group' value='1' checked={selected === '1'} onChange={e => setSelected(e.target.value)} /> Option 1
<input type='radio' name='group' value='2' checked={selected === '2'} onChange={e => setSelected(e.target.value)} /> Option 2
<p>Selected: {selected}</p>
</div>
);
}Solution
Step 1: Initial state is empty string
The stateselectedstarts as empty string, so no radio is checked initially.Step 2: User selects "Option 2" radio
On selecting "Option 2",onChangesetsselectedto '2'.Step 3: Component re-renders showing selected value
The paragraph showsSelected: 2because state updated to '2'.Final Answer:
Selected: 2 -> Option AQuick Check:
Radio selected value updates state and display [OK]
- Assuming state remains empty after selection
- Confusing checked with value attribute
- Expecting multiple radios to be selected
function Checkbox() {
const [checked, setChecked] = React.useState(false);
return (
<input type='checkbox' checked={checked} onChange={e => setChecked(e.target.value)} />
);
}Solution
Step 1: Check event handler usage
The onChange handler usese.target.value, which is a string, not boolean.Step 2: Correct property for checkbox state
Checkbox state must usee.target.checkedwhich is boolean true/false.Final Answer:
Using e.target.value instead of e.target.checked in onChange -> Option AQuick Check:
Checkbox onChange must use e.target.checked [OK]
- Using e.target.value for checkbox state
- Confusing onClick with onChange
- Not initializing state properly
Solution
Step 1: Understand multiple checkbox state needs
Multiple checkboxes require tracking which items are selected, best done with an array.Step 2: Update array state on checkbox toggle
On change, add topping if checked, remove if unchecked, keeping UI in sync.Final Answer:
Use an array state to store selected toppings and update it by adding/removing toppings on checkbox changes. -> Option CQuick Check:
Multiple checkboxes = array state with add/remove [OK]
- Using single boolean for multiple checkboxes
- Not updating UI after state change
- Concatenating strings instead of using arrays
Ready to prove it?
Pass the test and this pattern moves unknown
