Checkbox & Radio — Complete Guide
Checkbox & Radio — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 22 of 100
Checkbox & Radio
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Learn by example · ~6 min · Module 3: Forms & Hooks
What is this?
Checkboxes use checked={bool} and onChange to toggle. Radio buttons share a name and use checked={value === option} so only one is selected.
Why should you care?
Terms acceptance, gift wrap options, and payment method pickers use these controls.
See it live — copy this example
Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.
import { useState } from 'react';
function CheckoutOptions() {
const [giftWrap, setGiftWrap] = useState(false);
const [pay, setPay] = useState('upi');
return (
<>
<label>
<input type="checkbox" checked={giftWrap} onChange={e => setGiftWrap(e.target.checked)} />
Gift wrap
</label>
<label>
<input type="radio" name="pay" checked={pay === 'card'} onChange={() => setPay('card')} />
Card
</label>
<label>
<input type="radio" name="pay" checked={pay === 'upi'} onChange={() => setPay('upi')} />
UPI
</label>
</>
);
}
Run Example »
Edit the code and click Run to see the result update live.
What happened?
- checkbox uses e.target.checked.
- radio sets state to a string; checked compares current state to that option.
Try it yourself
- Add agree-to-terms checkbox — disable submit until checked.
- Three radio options for delivery speed.
- Log giftWrap and pay on a button click.
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
Remember
checkbox: checked + onChange. radio: same name, checked={state === value}. Wrap in label for bigger click area.