selectedFruits after checking 'Apple' and 'Banana'?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> ); }
When you check 'Apple' and 'Banana', the handleChange function adds their values to the selectedFruits array. So the array contains both.
selectedColor after the user clicks the 'Blue' option?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> ); }
When the user clicks 'Blue', the handleChange updates selectedColor to 'Blue'.
const [selectedItems, setSelectedItems] = useState([]); function handleCheckboxChange(event) { const { value, checked } = event.target; // Update state here }
Option A uses the functional updater prev => [...prev, value] which ensures the latest state is used, preventing bugs when state updates happen quickly.
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> ); }
Directly assigning size = event.target.value does not update React state. You must call setSize(event.target.value) to update state and re-render.
Controlled components keep the input state inside React, making UI predictable and easier to debug. The other options are incorrect because controlled inputs require event handlers and do not automatically optimize performance or delegate state to the browser.