Controlled Vs Uncontrolled Components In React? Complete Guide

Controlled components have their state managed by React.
The value of input elements is bound to state and updated via onChange.

import React, { useState } from "react";

function ControlledInput() {
  const [value, setValue] = useState("");

  return (
    <input
      type="text"
      value={value}
      onChange={(e) => setValue(e.target.value)}
    />
  );
}

✅ React fully controls the input value.


Uncontrolled Components

Uncontrolled components manage their own state internally.
Use ref to access the value when needed.

import React, { useRef } from "react";

function UncontrolledInput() {
  const inputRef = useRef();

  const handleClick = () => {
    alert(`Value: ${inputRef.current.value}`);
  };

  return (
    <div>
      <input type="text" ref={inputRef} />
      <button onClick={handleClick}>Show Value</button>
    </div>
  );
}

✅ Input manages its own state; React reads it via ref.


Key Differences

FeatureControlledUncontrolled
State ManagementManaged by React stateManaged by DOM
Value AccessDirect from stateUsing refs
UpdatesonChange handler requiredNo handler needed
Use CaseForms with validationSimple inputs, quick prototypes

Conclusion

Use controlled components for predictable form handling and validation, and uncontrolled components for simple or legacy inputs.


Citations

https://savanka.com/category/learn/react/
https://www.w3schools.com/REACT/DEFAULT.ASP

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *