Lesson 22/100

Tutorials React.js Tutorial

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

BeginnerIntermediateAdvancedProfessional

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.

Code
Result

What happened?

  • checkbox uses e.target.checked.
  • radio sets state to a string; checked compares current state to that option.

Try it yourself

  1. Add agree-to-terms checkbox — disable submit until checked.
  2. Three radio options for delivery speed.
  3. Log giftWrap and pay on a button click.
  4. Change text or labels in the example and save — watch the browser update.
  5. 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.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Mid PDF Detailed
Mock axios:?
Short answer: import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data' }); Example code import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data'…
Mid PDF Detailed
Choose a Testing Library: Use libraries like Jest (test runner) and React Testing?
Short answer: Library (for testing React components). Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, with hooks for local UI state. Say this in the…
Mid PDF Detailed
Only call hooks at the top level?
Short answer: (Don’t call hooks inside loops, conditions, or nested functions.) Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the int…
Mid PDF Detailed
Mock axios: import axios from 'axios'; jest.mock('axios');?
Short answer: xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); Re…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details