Skip to content

Controlled and uncontrolled inputs

BasicsConcept

An input box already remembers what you type, before React is involved. So who holds the real value: the DOM, or your state? You choose, and that choice decides what your code can do with it.

01 Two places a value can live

A plain HTML input is a DOM node with its own value. Type into it and the browser updates that value; no JavaScript needed. React state is a second copy of the value, kept in your component.

Two copies of one value is a recipe for disagreement, so React makes you pick one owner per input:

UncontrolledThe DOM owns itYou give a starting value with defaultValue, then read the box when you need it, through a ref or the form’s data.
ControlledReact owns itYou pass value from state and update state in onChange. The box shows whatever state says, on every render.

02 The same form, both ways

Both forms save a name. The uncontrolled one reads the box once, on submit. The controlled one always has the current text in name, so it can show a counter and disable the button while you type.

Uncontrolled
function SignupForm() {
const nameRef = useRef(null);
function handleSubmit(e) {
e.preventDefault();
save(nameRef.current.value);
}
return (
<form onSubmit={handleSubmit}>
<input ref={nameRef} defaultValue="An" />
<button>Save</button>
</form>
);
}
Controlled
function SignupForm() {
const [name, setName] = useState('An');
function handleSubmit(e) {
e.preventDefault();
save(name);
}
return (
<form onSubmit={handleSubmit}>
<input value={name}
onChange={e => setName(e.target.value)} />
<p>{name.length} / 20</p>
<button disabled={name === ''}>Save</button>
</form>
);
}

Note the attribute. defaultValue sets the box once, when it first appears; changing it later does nothing. value is applied on every render.

03 What happens on one keystroke

In a controlled input the browser still changes the box first; it doesn’t know about React. What makes the input controlled is that React checks the box against state after your handler runs. Follow one letter through.

One keystroke in a controlled input
Step 1 of 6
DOM input · what the box shows
An
React state
name→"An"
What happened, in order
Empty
  • changed by the browser
  • read, not changed
  • just happened
  • in sync

The component has rendered with name set to "An", so the box shows An. Nothing has happened yet.

If your onChange doesn’t update state, React sets the box back to the old value after the event. That is why an input with value and no working onChange looks frozen: every key you press is undone.

04 React has the last word

Because state decides what is shown, the handler can change the text before storing it. This box upper-cases what you type and stops at 6 characters, with no flicker of the raw text:

JavaScript
const [code, setCode] = useState('');
<input
value={code}
onChange={e => setCode(e.target.value.toUpperCase().slice(0, 6))}
/>

An uncontrolled input can’t do this cleanly. You would have to rewrite ref.current.value by hand after the browser has already shown the raw text.

05 Which one to use

Pick the owner by what your code needs
You need to…Use
Show live feedback: a counter, a validation message, a previewControlled
Change or block what is typed (upper-case, digits only)Controlled
Set or clear the box from code, e.g. after savingControlled
Read the value only on submitUncontrolled is enough
Accept a file with input type="file"Uncontrolled: its value can’t be set from code

Controlled is the usual default in React code, because the value is always in state where the rest of the component can use it. Uncontrolled inputs are simpler for a field you only read once, and some form libraries use them to avoid re-rendering on every key.

06 Where it goes wrong

<input value={name} />A value with no onChangeThe box is read-only: React restores it after every key. In development React warns: “You provided a value prop to a form field without an onChange handler. This will render a read-only field.” Add an onChange, or use defaultValue if you didn’t mean to control it.
<input value={user.nickname} … />A value that starts as undefinedWith undefined, React treats the input as uncontrolled. When the value arrives, it switches to controlled and warns: “A component is changing an uncontrolled input to be controlled.” Start with a string: value={user.nickname ?? ''}.
<input defaultValue={draft.title} />Expecting defaultValue to updateIt only applies on the first render. If draft changes, the box keeps the old text. Control the input, or give it a key so React builds a fresh one.
<input type="checkbox" value={done} />Using value on a checkboxA checkbox is controlled by checked (or defaultChecked). Its value is the text sent with the form, not whether it is ticked.

07 Check yourself

Question

An input has value={city} and onChange={e => console.log(e.target.value)}. You type x. What does the box show afterwards?

Question

You render <input defaultValue={name} />. Later name changes from "An" to "Bo". What does the box show?

Question

You want the Save button disabled while the name box is empty. Which input fits best?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?