Bird
Raised Fist0
Reactframework~10 mins

Handling checkboxes and radio buttons in React - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a checkbox input that updates state when clicked.

React
const [checked, setChecked] = useState(false);

return (
  <input type="checkbox" checked={checked} onChange=[1] />
);
Drag options to blanks, or click blank then click option'
A() => setChecked(!checked)
B() => setChecked(true)
C() => setChecked(false)
D() => setChecked(checked)
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the state always to true or false instead of toggling.
Not using a function to update the state.
2fill in blank
medium

Complete the code to handle a radio button group that updates the selected option in state.

React
const [selected, setSelected] = useState('option1');

return (
  <input type="radio" value="option1" checked={selected === 'option1'} onChange=[1] />
);
Drag options to blanks, or click blank then click option'
A(e) => setSelected(true)
B() => setSelected('option1')
C(e) => setSelected(e.target.value)
D() => setSelected(false)
Attempts:
3 left
💡 Hint
Common Mistakes
Setting state to a boolean instead of the value string.
Not using the event parameter to get the value.
3fill in blank
hard

Fix the error in the checkbox handler to correctly update state based on the checkbox's checked property.

React
const [checked, setChecked] = useState(false);

function handleChange(e) {
  setChecked([1]);
}

return <input type="checkbox" checked={checked} onChange={handleChange} />;
Drag options to blanks, or click blank then click option'
Ae.target.value
Be.checked
Ce.checkedValue
De.target.checked
Attempts:
3 left
💡 Hint
Common Mistakes
Using e.target.value which is a string, not boolean.
Using non-existent properties like e.checked.
4fill in blank
hard

Fill both blanks to create a controlled radio button group that updates state on change.

React
const [choice, setChoice] = useState('A');

return (
  <input type="radio" value="A" checked={choice [1] 'A'} onChange=[2] />
);
Drag options to blanks, or click blank then click option'
A===
B!==
C(e) => setChoice(e.target.value)
D() => setChoice('A')
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of === for checked comparison.
Using a static function that does not use event to update state.
5fill in blank
hard

Fill the two blanks to create a checkbox group that updates an array of selected options in state.

React
const [selected, setSelected] = useState([]);

function handleCheckboxChange(e) {
  const value = e.target.value;
  if (selected.includes(value)) {
    setSelected(selected.filter(item => item [1] value));
  } else {
    setSelected([...selected, [2]]);
  }
}

return (
  <input type="checkbox" value="opt1" checked={selected.includes('opt1')} onChange={handleCheckboxChange} />
);
Drag options to blanks, or click blank then click option'
A!==
Bvalue
C===
Dselected
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of !== in filter, which would keep the item instead of removing it.
Adding the whole selected array instead of the new value.

Practice

(1/5)
1. Which statement best describes the difference between checkboxes and radio buttons in React?
easy
A. Both checkboxes and radio buttons allow only one selection.
B. Checkboxes allow only one selection; radio buttons allow multiple selections.
C. Both checkboxes and radio buttons allow multiple selections.
D. Checkboxes allow multiple selections; radio buttons allow only one selection.

Solution

  1. Step 1: Understand checkbox behavior

    Checkboxes let users select any number of options, including none, one, or many.
  2. Step 2: Understand radio button behavior

    Radio buttons restrict selection to exactly one option within a group.
  3. Final Answer:

    Checkboxes allow multiple selections; radio buttons allow only one selection. -> Option D
  4. Quick Check:

    Checkboxes = multiple, Radio buttons = single [OK]
Hint: Checkboxes = many choices, radios = one choice [OK]
Common Mistakes:
  • Confusing which input allows multiple selections
  • Thinking radio buttons allow multiple selections
  • Assuming checkboxes allow only one selection
2. Which of the following is the correct way to handle a checkbox change event in React functional components?
easy
A. <input type='checkbox' checked={checked} onClick={e => setChecked(e.target.checked)} />
B. <input type='checkbox' checked={checked} onChange={e => setChecked(e.target.checked)} />
C. <input type='checkbox' value={checked} onChange={e => setChecked(e.target.checked)} />
D. <input type='checkbox' checked={checked} onChange={e => setChecked(e.target.value)} />

Solution

  1. Step 1: Use checked attribute for checkbox state

    Checkbox uses checked prop to reflect its state, not value.
  2. Step 2: Use onChange event and e.target.checked

    The onChange event provides e.target.checked boolean to update state.
  3. Final Answer:

    <input type='checkbox' checked={checked} onChange={e => setChecked(e.target.checked)} /> -> Option B
  4. Quick Check:

    Checkbox state = checked + onChange with e.target.checked [OK]
Hint: Use checked and onChange with e.target.checked [OK]
Common Mistakes:
  • Using e.target.value instead of e.target.checked
  • Using onClick instead of onChange for checkbox
  • Binding value instead of checked attribute
3. What will be the output when the following React component is rendered and the user selects the "Option 2" radio button?
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>
  );
}
medium
A. Selected: 2
B. Selected: '' (empty string)
C. Selected: 1
D. No output, component crashes

Solution

  1. Step 1: Initial state is empty string

    The state selected starts as empty string, so no radio is checked initially.
  2. Step 2: User selects "Option 2" radio

    On selecting "Option 2", onChange sets selected to '2'.
  3. Step 3: Component re-renders showing selected value

    The paragraph shows Selected: 2 because state updated to '2'.
  4. Final Answer:

    Selected: 2 -> Option A
  5. Quick Check:

    Radio selected value updates state and display [OK]
Hint: Radio updates state with value, display shows selected [OK]
Common Mistakes:
  • Assuming state remains empty after selection
  • Confusing checked with value attribute
  • Expecting multiple radios to be selected
4. Identify the error in this React checkbox handler code:
function Checkbox() {
  const [checked, setChecked] = React.useState(false);
  return (
    <input type='checkbox' checked={checked} onChange={e => setChecked(e.target.value)} />
  );
}
medium
A. Using e.target.value instead of e.target.checked in onChange
B. Missing initial state for checked
C. Using onClick instead of onChange event
D. Not binding the checked attribute

Solution

  1. Step 1: Check event handler usage

    The onChange handler uses e.target.value, which is a string, not boolean.
  2. Step 2: Correct property for checkbox state

    Checkbox state must use e.target.checked which is boolean true/false.
  3. Final Answer:

    Using e.target.value instead of e.target.checked in onChange -> Option A
  4. Quick Check:

    Checkbox onChange must use e.target.checked [OK]
Hint: Checkbox onChange uses e.target.checked, not value [OK]
Common Mistakes:
  • Using e.target.value for checkbox state
  • Confusing onClick with onChange
  • Not initializing state properly
5. You want to create a React component that lets users select multiple toppings using checkboxes and shows the selected toppings below. Which approach correctly manages multiple checkbox states?
hard
A. Use separate state variables for each checkbox and ignore updating the UI on change.
B. Use a single boolean state for all checkboxes and toggle it on any change.
C. Use an array state to store selected toppings and update it by adding/removing toppings on checkbox changes.
D. Use a string state to store all toppings concatenated and update it by appending toppings on change.

Solution

  1. Step 1: Understand multiple checkbox state needs

    Multiple checkboxes require tracking which items are selected, best done with an array.
  2. Step 2: Update array state on checkbox toggle

    On change, add topping if checked, remove if unchecked, keeping UI in sync.
  3. Final Answer:

    Use an array state to store selected toppings and update it by adding/removing toppings on checkbox changes. -> Option C
  4. Quick Check:

    Multiple checkboxes = array state with add/remove [OK]
Hint: Use array state to track multiple checkbox selections [OK]
Common Mistakes:
  • Using single boolean for multiple checkboxes
  • Not updating UI after state change
  • Concatenating strings instead of using arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard