Sub topic 7
Uncontrolled inputs and refs
This chapter assumes you know useState, JSX event handlers, and how the earlier chapters built controlled inputs with value and onChange.
What "uncontrolled" means
An uncontrolled input keeps its current text in the browser's DOM, not in React state. React renders the element once and does not track each keystroke. You read the value only when you need it, for example when the user submits the form.
In a controlled input, React state is the source of truth. In an uncontrolled input, the DOM is.
Setting a starting value with defaultValue
If you pass value without onChange, React makes the input read-only and warns you. For an uncontrolled input, leave value out. Use defaultValue to set the starting text. React applies it on the first render and then ignores it.
For checkboxes and radios, the matching prop is defaultChecked.
Reading the value with a ref
A ref is an object, created with useRef, that React keeps the same across renders. If you attach it to an element with the ref prop, React sets ref.current to the real DOM node. You can then read ref.current.value whenever you want.
import { useRef } from 'react';
export default function NewsletterForm() {
const emailRef = useRef(null);
function handleSubmit(e) {
e.preventDefault();
console.log('Subscribing', emailRef.current.value);
}
return (
<form onSubmit={handleSubmit}>
<label>
Email
<input ref={emailRef} type="email" defaultValue="" />
</label>
<button type="submit">Subscribe</button>
</form>
);
}Typing in this field causes no re-render. The component runs once, and the handler reads the DOM when the user submits.
Refs also let you call DOM methods. A common use is moving focus to the first invalid field:
emailRef.current.focus();Common errors
- Reading
ref.currenttoo early. The ref isnulluntil React mounts the element. Read it inside event handlers or effects, not during rendering. - Mixing both styles. Passing
valueanddefaultValueto the same input triggers a React warning. Pick one. - Switching modes. If
valuestarts asundefinedand later becomes a string, React warns that the input changed from uncontrolled to controlled. Initialize state with'', notundefined. - Expecting
defaultValueto update. ChangingdefaultValueafter mount does not change the displayed text.
When to choose uncontrolled
Uncontrolled inputs fit when you only need the values at submit time, when the form is simple, or when you integrate with non-React code that manages the DOM. They also avoid re-rendering the component on every keystroke. A file input must always be uncontrolled, because browsers do not let a script set its value.
The trade-off is less control. Live validation, formatting as the user types, and conditional fields are harder without state. With many fields, one ref per input also gets tedious. The next chapter shows how FormData reads every named field at once and removes the need for most refs.
Exercise
A developer writes <input value="hello" /> with no onChange, and the user cannot type in it. What are two ways to fix it, and how do they differ?
Show answer
Use defaultValue="hello" to make it uncontrolled. The text starts as "hello", the user can edit it, and you read it later through a ref. Or add state and an onChange handler, for example value={text} with onChange={(e) => setText(e.target.value)}, to make it controlled. Then React state holds the text and re-renders on each keystroke.