Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ React Mastery Series β€” Day 18

Published
β€’3 min read
πŸ“˜ React Mastery Series β€” Day 18
S

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.

Controlled vs Uncontrolled Components β€” Forms Mastery in React

Forms are everywhere:

  • Login pages

  • Signup forms

  • Search inputs

  • Checkout pages

But in React, forms work differently than plain HTML.

Today we understand:

πŸ‘‰ Controlled Components
πŸ‘‰ Uncontrolled Components


🧠 1️⃣ Controlled Components

πŸ“Œ Definition

A Controlled Component is a form element whose value is controlled by React state.

React controls the input.


πŸ›  Example β€” Controlled Input

import { useState } from "react";

function ControlledForm() {
  const [name, setName] = useState("");

  const handleSubmit = (e) => {
    e.preventDefault();
    alert(`Submitted Name: ${name}`);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
        placeholder="Enter your name"
      />
      <button type="submit">Submit</button>
    </form>
  );
}

πŸ”Ž What’s happening?

  • Input value comes from state

  • onChange updates state

  • React fully controls the input

That’s why it's called Controlled.


βœ… Advantages of Controlled Components

βœ” Easy validation
βœ” Real-time error handling
βœ” Better control over form data
βœ” Predictable behavior

Most real-world apps use this pattern.


🧠 2️⃣ Uncontrolled Components

πŸ“Œ Definition

An Uncontrolled Component stores its own internal state in the DOM.

React does NOT control the input directly.

We use useRef to access its value.


πŸ›  Example β€” Uncontrolled Input

import { useRef } from "react";

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

  const handleSubmit = (e) => {
    e.preventDefault();
    alert(`Submitted Name: ${inputRef.current.value}`);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input type="text" ref={inputRef} placeholder="Enter your name" />
      <button type="submit">Submit</button>
    </form>
  );
}

Here:

  • Value is stored in DOM

  • React reads it using ref

  • No state management


πŸ”₯ Key Difference

ControlledUncontrolled
Managed by React stateManaged by DOM
Uses useStateUses useRef
More predictableLess control
Best for complex formsSimple quick forms

🎯 When to Use What?

βœ” Use Controlled for:

  • Login / Signup forms

  • Validation-heavy forms

  • Dynamic form behavior

βœ” Use Uncontrolled for:

  • Simple input fields

  • Quick integrations

  • When you don’t need validation


🧠 Interview One-Liner

Controlled components are managed by React state, while uncontrolled components rely on the DOM to manage their own state.


πŸš€ Real-World Insight

In professional projects:

πŸ‘‰ 90% of the time you will use Controlled Components.

Because validation and dynamic UI are very common.


βœ… Key Takeaways

βœ” Controlled = React controls input
βœ” Uncontrolled = DOM controls input
βœ” Controlled is more powerful
βœ” Uncontrolled is simpler


πŸ”œ Coming Next

Day 19 β€” Lifting State Up (Component Communication Mastery)

Now we level up communication between components πŸ”₯

More from this blog