Member-only story
Forms capture some of the most dynamic pieces of application state. In React, uncontrolled components provide a straightforward way to manage form data in local mutable variables instead of state. Ditching boilerplate handlers unlocks cleaner form code.
Let’s see how embracing uncontrolled inputs simplifies workflows and boosts performance.
Controlled Components Recap
First, React recommends form inputs utilize controller components to facilitate state lifting into React state:
function Form() {
const [name, setName] = useState('Mary');
const handleChange = e => {
setName(e.target.value);
}
return (
<>
<input
value={name}
onChange={handleChange}
/>
<div>
Hello {name}!
</div>
</>
);
}
Here the name input stays in sync with React state. This controlled approach allows easy data lifting while ensuring UI consistency.
The Hits of Controlled Forms
However, controlled inputs come with downsides:
- Lots of repetitive change handlers