Sub topic 12
Controlled inputs for the search box and checkbox
This chapter assumes you know JSX, function components, props, and useState. It also assumes you have seen how FilterableProductTable owns the state and passes setter functions down as callbacks. Here we look at how SearchBar turns its two inputs into controlled inputs.
What "controlled" means
A form input keeps its own value in the browser. A controlled input hands that job to React. The component gives the input its current value as a prop. When the user types, the input reports the change, and the parent updates state. React then re-renders and gives the input the new value.
The input never decides its own value. It only displays what it was given.
The SearchBar
SearchBar holds no state. It receives two values and two callbacks as props.
function SearchBar({
filterText,
inStockOnly,
onFilterTextChange,
onInStockOnlyChange
}) {
return (
<form>
<input
type="text"
value={filterText}
placeholder="Search..."
onChange={(e) => onFilterTextChange(e.target.value)}
/>
<label>
<input
type="checkbox"
checked={inStockOnly}
onChange={(e) => onInStockOnlyChange(e.target.checked)}
/>{' '}
Only show products in stock
</label>
</form>
);
}Two details matter here.
- The text input uses
value, and the checkbox useschecked. Usingvalueon a checkbox is a common mistake. A checkbox's on/off state is inchecked. - The handlers read different fields. For text, read
e.target.value. For a checkbox, reade.target.checked.
Wiring it to the parent
The parent supplies both the values and the callbacks.
import { useState } from 'react';
function FilterableProductTable({ products }) {
const [filterText, setFilterText] = useState('');
const [inStockOnly, setInStockOnly] = useState(false);
return (
<div>
<SearchBar
filterText={filterText}
inStockOnly={inStockOnly}
onFilterTextChange={setFilterText}
onInStockOnlyChange={setInStockOnly}
/>
<ProductTable
products={products}
filterText={filterText}
inStockOnly={inStockOnly}
/>
</div>
);
}Passing setFilterText directly works because the setter accepts the new value as its only argument,earchBar calls it with. The same state feeds both SearchBar and ProductTable`, so the input and the table always agree.
Common errors
| Symptom | Likely cause |
|---|---|
| Typing in the box does nothing | `value` is set but there is no `onChange`, or the handler never updates state |
| Checkbox cannot be toggled | Using `value` instead of `checked`, or ignoring `e.target.checked` |
| Warning about switching from uncontrolled to controlled | The value started as `undefined` and later became a string. Initialize state with `''` or `false` |
| Table does not update | State lives in `SearchBar` instead of the common parent, so `ProductTable` never sees it |
React shows a warning when you pass value without onChange. The input becomes read-only, because React resets it to the prop after every keystroke.
Why this design pays off
Because the input value lives in state, you can read it anywhere in the parent. Filtering, clearing the box, or setting a default all become ordinary state updates. A "Clear" button only needs setFilterText(''), and the input will show an empty string on the next render.
Exercise
A teammate writes <input type="checkbox" value={inStockOnly} onChange={(e) => onInStockOnlyChange(e.target.value)} />. The box does not behave as expected. What two changes fix it?
Show answer
Change value={inStockOnly} to checked={inStockOnly}, and change e.target.value to e.target.checked. A checkbox stores its on/off state in checked, and e.target.value is a string that does not reflect whether the box is ticked.
Exercise
You want a "Clear" button inside FilterableProductTable that empties the search box. What does its click handler do, and why does the input update?
Show answer
The handler calls setFilterText(''). That changes state, so React re-renders FilterableProductTable. SearchBar receives filterText="" and passes it as the input's value, so the box shows empty.