Sub topic 7
Controlled inputs: the parent owns the value
This chapter assumes you know useState, props, and onChange handlers. It explains what makes an input "controlled" and why that is the form lifting state up takes for form fields.
What "controlled" means
A form element such as <input> keeps its own text in the browser DOM. An input is controlled when React supplies its value and the component updates that value through an onChange handler. React's state becomes the source of truth, and the DOM only displays it.
An input is uncontrolled when you give it no value prop. The browser then owns the text, and React only sees it if you read it later.
Controlled inputs matter for lifting state up because a parent can own the value and hand it to several inputs at once. In the earlier sibling example, each Field had its own useState, so the two inputs drifted apart. Making Field controlled removes that local state.
The data loop
A controlled input forms a loop:
- The parent renders the input with
value={text}. - The user types, and the input fires
onChange. - The handler calls the parent's
setTextwith the new string. - React re-renders the parent, and every input receives the new
value.
A controlled Field
This version of Field has no state of its own. It receives value and onChange as props.
import { useState } from 'react';
function Field({ label, value, onChange }) {
return (
<label>
{label}: <input value={value} onChange={e => onChange(e.target.value)} />
</label>
);
}
export default function App() {
const [text, setText] = useState('');
return (
<>
<Field label="First" value={text} onChange={setText} />
<Field label="Second" value={text} onChange={setText} />
</>
);
}Typing in either input calls setText. App re-renders and passes the same string to both fields, so they always match.
Two details are worth noting:
Fieldpassese.target.valuetoonChange, not the whole event. The parent only needs the string, so the child's interface stays small and does not depend on the DOM event.- Because
onChangeissetText, the parent could instead pass a function that validates or transforms the string before storing it.
Why controlled inputs help
When the parent owns the value, it can do more than mirror it:
- Format the text, for example
setText(v.toUpperCase()). - Reject input, for example ignore non-digits.
- Reset the field by calling
setText(''). - Use the value in other places, such as a character counter or a disabled submit button.
An uncontrolled input cannot do these things without reading the DOM.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Input will not accept typing | `value` is set but no `onChange` is given, so the value never changes | Add an `onChange` that updates state |
| Warning about switching from uncontrolled to controlled | `value` starts as `undefined` and later becomes a string | Initialize state with `''`, not `undefined` |
| Number input behaves oddly | `e.target.value` is always a string | Convert it with `Number()` or `parseFloat()` when you need a number |
| Both inputs show different text | Each field still has its own `useState` | Remove the local state and use the props |
Exercise
A parent renders <input value={name} /> with const [name, setName] = useState('Ada') and no onChange. What happens when the user types, and how do you fix it?
Show answer
The input stays at "Ada" and ignores typing, because React keeps forcing the value to name and nothing updates it. React also logs a warning. Fix it by adding onChange={e => setName(e.target.value)}.
Exercise
Change the controlled App above so that both inputs always show the text in uppercase, whichever one the user types in.
Show answer
Pass a transforming callback: onChange={v => setText(v.toUpperCase())} on both Field components. The parent owns the value, so it can transform it before storing it.