Skip to main content

Command Palette

Search for a command to run...

Controlled vs Uncontrolled components in react

Published
โ€ข1 min read
B

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.

  1. Controlled Component

    A controlled component is a form element whose value is managed by React state.
    The input value is passed via the value prop, and updates happen through an onChange handler.

  2. 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 a ref.


๐Ÿ”‘ 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} />

More from this blog

Bikash Mainali

75 posts

Experienced Java/JavaScript full-stack developer with 8 years of extensive expertise serving key role on elite technical teams developing and designing software.