Controlled vs Uncontrolled components in react
Experienced Java/JavaScript full-stack developer over 6 years of extensive expertise serving key role on elite technical teams developing enterprise software for healthcare, apple ad-platform, banking, and e-commerce. Adaptable problem-solver with high levels of skill in Groovy, Java, Spring, Spring Boot, Hibernate, JavaScript, TypeScript, Angular, Node, Express, React, MongoDB, IBM DB2, Oracle, PL/SQL, Docker, Kubernetes, CI/CD pipelines, AWS, Micro-service and Agile/Scrum. Strong technical skills paired with business-savvy UI design expertise. Personable team player with experience collaborating with diverse cross-functional teams.
Controlled Component
A controlled component is a form element whose value is managed by React state.
The input value is passed via thevalueprop, and updates happen through anonChangehandler.Uncontrolled Component
An uncontrolled component is a form element that manages its own state in the DOM.
React accesses the value only when needed using aref.
๐ Key Difference (One-Line Answer)
Controlled components are driven by React state, while uncontrolled components rely on the DOM for storing input values.
How They Work
Controlled Flow
User types โ onChange โ setState โ re-render โ updated value
Uncontrolled Flow
User types โ DOM stores value โ React reads via ref when needed
Code Comparison
Controlled Example
const [name, setName] = useState("");
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
Uncontrolled Example
const nameRef = useRef();
<input ref={nameRef} />