Skip to content
react questions

React · 9 of 13junior~1 min

What are controlled and uncontrolled components?

#forms#core

Controlled vs Uncontrolled components

Controlled Component

The field value is stored in React state. React fully controls the form.

tsx
function ControlledInput() { const [value, setValue] = useState(''); return ( <input value={value} onChange={(e) => setValue(e.target.value)} /> ); }

Uncontrolled Component

The value is stored in the DOM. Read via ref.

tsx
function UncontrolledInput() { const ref = useRef<HTMLInputElement>(null); const handleSubmit = () => { console.log(ref.current?.value); }; return <input ref={ref} defaultValue="default" />; }

When to use which

ControlledUncontrolled
Live validationYesNo
Dependent fieldsYesNo
Simple formsOverkillYes
Non-React integrationHardYes

Most React forms are controlled. Uncontrolled is used for file inputs and integration with third-party DOM libraries.

updated

did you know it?

related questions