How do you track the state of a checkbox in React?
You use the useState hook to store a boolean value representing whether the checkbox is checked or not. The checkbox's checked attribute is set from this state, and the onChange handler updates the state when the user clicks.
Click to reveal answer
intermediate
What is the difference between controlled and uncontrolled checkboxes in React?
Controlled checkboxes have their checked state managed by React state, so React controls their value. Uncontrolled checkboxes manage their own state internally, and you access their value using refs or form submission.
Click to reveal answer
intermediate
How do you handle multiple checkboxes in React when users can select many options?
You store the selected options in an array state. When a checkbox is checked, you add its value to the array. When unchecked, you remove it. The checked attribute checks if the value is in the array.
Click to reveal answer
beginner
How do radio buttons differ from checkboxes in React handling?
Radio buttons allow only one selection in a group. You store a single value in state representing the selected option. Each radio button's checked attribute compares its value to the state value.
Click to reveal answer
beginner
Why is it important to use the name attribute on radio buttons?
The name attribute groups radio buttons so the browser knows they belong together. This ensures only one radio button in the group can be selected at a time and improves accessibility.
Click to reveal answer
Which React hook is commonly used to manage checkbox state?
AuseContext
BuseEffect
CuseState
DuseRef
✗ Incorrect
useState is used to store and update the checkbox's checked state.
How do you represent multiple selected checkboxes in React state?
AAn array of selected values
BA string
CAn object with keys true/false
DA boolean value
✗ Incorrect
An array holds all selected checkbox values for multiple selections.
What attribute must radio buttons share to be grouped correctly?
Achecked
Bvalue
Cid
Dname
✗ Incorrect
The name attribute groups radio buttons so only one can be selected.
In React, how do you update the state when a checkbox is clicked?
AUse onChange handler to update state
BDirectly change the DOM element
CUse useEffect to listen for clicks
DUse setTimeout to delay update
✗ Incorrect
The onChange handler updates the React state when the checkbox changes.
What happens if you don't control a checkbox in React?
AReact manages its state automatically
BThe checkbox manages its own state (uncontrolled)
CThe checkbox cannot be clicked
DThe checkbox will always be checked
✗ Incorrect
Without controlling it, the checkbox manages its own state internally.
Explain how to handle a single checkbox in React using state.
Think about how you track if the box is checked or not.
You got /4 concepts.
Describe how to manage a group of radio buttons in React so only one can be selected.
Remember radio buttons work like a team where only one can win.
You got /4 concepts.
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
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 D
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
Step 1: Use checked attribute for checkbox state
Checkbox uses checked prop to reflect its state, not value.
Step 2: Use onChange event and e.target.checked
The onChange event provides e.target.checked boolean to update state.
Final Answer:
<input type='checkbox' checked={checked} onChange={e => setChecked(e.target.checked)} /> -> Option B
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?
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
Step 1: Check event handler usage
The onChange handler uses e.target.value, which is a string, not boolean.
Step 2: Correct property for checkbox state
Checkbox state must use e.target.checked which is boolean true/false.
Final Answer:
Using e.target.value instead of e.target.checked in onChange -> Option A
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.