0
0
Reactframework~20 mins

Handling checkboxes and radio buttons in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Checkbox & Radio Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when selecting multiple checkboxes?
Consider this React component that manages multiple checkboxes. What will be the value of selectedFruits after checking 'Apple' and 'Banana'?
React
import React, { useState } from 'react';

function FruitSelector() {
  const [selectedFruits, setSelectedFruits] = useState([]);

  function handleChange(event) {
    const { value, checked } = event.target;
    if (checked) {
      setSelectedFruits(prev => [...prev, value]);
    } else {
      setSelectedFruits(prev => prev.filter(fruit => fruit !== value));
    }
  }

  return (
    <form>
      <label><input type="checkbox" value="Apple" onChange={handleChange} /> Apple</label>
      <label><input type="checkbox" value="Banana" onChange={handleChange} /> Banana</label>
      <label><input type="checkbox" value="Cherry" onChange={handleChange} /> Cherry</label>
    </form>
  );
}
A["Banana"]
B["Apple"]
C["Apple", "Banana"]
D[]
Attempts:
2 left
💡 Hint
Think about how the state array changes when checkboxes are checked or unchecked.
state_output
intermediate
2:00remaining
What is the selected radio button value after user clicks?
Given this React component with radio buttons, what will be the value of selectedColor after the user clicks the 'Blue' option?
React
import React, { useState } from 'react';

function ColorPicker() {
  const [selectedColor, setSelectedColor] = useState('Red');

  function handleChange(event) {
    setSelectedColor(event.target.value);
  }

  return (
    <form>
      <label><input type="radio" value="Red" checked={selectedColor === 'Red'} onChange={handleChange} /> Red</label>
      <label><input type="radio" value="Blue" checked={selectedColor === 'Blue'} onChange={handleChange} /> Blue</label>
      <label><input type="radio" value="Green" checked={selectedColor === 'Green'} onChange={handleChange} /> Green</label>
    </form>
  );
}
A"Red"
B"Blue"
C"Green"
Dnull
Attempts:
2 left
💡 Hint
Radio buttons allow only one selection, so the state updates to the clicked value.
📝 Syntax
advanced
2:30remaining
Which option correctly handles multiple checkbox state updates?
You want to update the state array when checkboxes are toggled. Which code snippet correctly updates the state without causing stale state bugs?
React
const [selectedItems, setSelectedItems] = useState([]);

function handleCheckboxChange(event) {
  const { value, checked } = event.target;
  // Update state here
}
A
if (checked) {
  setSelectedItems(prev =&gt; [...prev, value]);
} else {
  setSelectedItems(prev =&gt; prev.filter(item =&gt; item !== value));
}
B
if (checked) {
  setSelectedItems([...selectedItems, value]);
} else {
  setSelectedItems(selectedItems.filter(item =&gt; item !== value));
}
C
if (checked) {
  selectedItems.push(value);
  setSelectedItems(selectedItems);
} else {
  const index = selectedItems.indexOf(value);
  if (index &gt; -1) selectedItems.splice(index, 1);
  setSelectedItems(selectedItems);
}
D
if (checked) {
  setSelectedItems(selectedItems.concat(value));
} else {
  setSelectedItems(selectedItems.filter(item =&gt; item !== value));
}
Attempts:
2 left
💡 Hint
Using the updater function form of setState avoids stale closures.
🔧 Debug
advanced
2:30remaining
Why does this radio button group not update on click?
This React component renders radio buttons but the selection never changes when clicked. What is the cause?
React
import React, { useState } from 'react';

function SizeSelector() {
  const [size, setSize] = useState('Medium');

  function handleChange(event) {
    setSize(event.target.value);
  }

  return (
    <form>
      <label><input type="radio" value="Small" checked={size === 'Small'} onChange={handleChange} /> Small</label>
      <label><input type="radio" value="Medium" checked={size === 'Medium'} onChange={handleChange} /> Medium</label>
      <label><input type="radio" value="Large" checked={size === 'Large'} onChange={handleChange} /> Large</label>
    </form>
  );
}
AThe state variable 'size' is assigned directly instead of using setSize, so React does not re-render.
BThe 'checked' attribute should be 'defaultChecked' for controlled inputs.
CThe event handler is missing 'event.preventDefault()' causing default form submission.
DThe radio buttons need unique 'name' attributes to group correctly.
Attempts:
2 left
💡 Hint
State variables in React must be updated with their setter functions to trigger re-render.
🧠 Conceptual
expert
3:00remaining
Why use controlled components for checkboxes and radio buttons in React?
Which statement best explains the advantage of using controlled components for checkboxes and radio buttons in React?
AControlled components eliminate the need for event handlers on inputs.
BControlled components automatically optimize rendering performance without extra code.
CControlled components allow the browser to manage state internally, reducing React's workload.
DControlled components keep the React state as the single source of truth, enabling predictable UI and easier debugging.
Attempts:
2 left
💡 Hint
Think about how React manages UI and state synchronization.