Skip to main content

Controlled vs. Uncontrolled Components in React.js: What's the Difference?


When working with forms in React, you’ll often come across two terms: controlled and uncontrolled components. Understanding the difference between them is essential for writing clean, predictable, and maintainable React applications.

In this blog post, I’ll break down the concept of controlled vs uncontrolled components in a simple, beginner-friendly way, with practical code examples to help you decide when to use which.

๐Ÿง  What Are Controlled Components?


  • A controlled component is a form element (like <input>, <textarea>, <select>) whose value is controlled by React state.

✅ Characteristics:


  • React state is the “single source of truth”
  • Every keystroke updates state via onChange
  • Ideal when you want to validate input, show live previews, or enforce formats
  • Used for complex form
  • Rerenderer overhead
  • Immediate validation.

๐Ÿ’ก Example:


import React, { useState } from "react";
function ControlledForm() {
const [name, setName] = useState("");
const handleChange = (e) => {
setName(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted Name: ${name}`);
};
return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input type="text" value={name} onChange={handleChange} />
</label>
<button type="submit">Submit</button>
</form>

);
}

๐Ÿ” How it works:


  • value={name} binds the input value to React state
  • onChange updates the state on every keystroke

๐Ÿค– What Are Uncontrolled Components?


An uncontrolled component is one where the form data is handled by the DOM itself. React doesn’t manage the input’s value directly — you access the value using ref.

✅ Characteristics:


  • You use ref to read values
  • No real-time state tracking
  • Good for quick form handling or integrating with non-React code.
  • Simple
  • Quick
  • Less control

๐Ÿ’ก Example:


import React, { useRef } from "react";
function UncontrolledForm() {
const nameRef = useRef();
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted Name: ${nameRef.current.value}`);
};
return (
<form onSubmit={handleSubmit}>
<label>
Name: <input type="text" ref={nameRef} />
</label>
<button type="submit">Submit</button>
</form>

);
}

๐Ÿ” How it works:


  • ref gives direct access to the DOM element
  • You get the value when needed (not on every keystroke)

๐ŸŽฏ Conclusion


Controlled components give you full control over your form inputs via state, enabling things like validation, conditional rendering, and live feedback. On the other hand, uncontrolled components are simpler and rely on the browser’s default behavior, making them ideal for less interactive forms or quick scripts.

Understanding when and why to use each type helps you write more effective and optimized React code.

๐Ÿ“Œ keep building, and React on! ⚛️


Comments

Popular posts from this blog

Exploring Google’s New Gemini CLI: The Ultimate Open-Source Dev Tool

  Google quietly released a local AI agent that builds apps, debugs code, parses your repo, and fetches real-time data, right inside your terminal. And it’s completely free. This year, the most revolutionary developer tools I’ve used didn’t come with a splashy launch or billion-dollar hype. It came as a simple CLI: Gemini CLI, a terminal-based AI agent built on top of Google’s Gemini 2.5 Pro model . At first glance, it looks like a lightweight alternative to Claude Code. But after just 10 minutes of use, it became clear: this isn’t just a convenient utility. It’s a powerful local AI development assistant that can analyze, automate, and accelerate almost every part of your software workflow. And best of all? It’s fully open-source under the Apache 2.0 license It gives you up to 1,000 free requests per day It integrates with your local filesystem, IDE, and the web And it runs entirely in your terminal , no browser needed In this guide, I’ll show you what Gemini CLI is, how it works...

Top 25 JavaScript Array Methods Every Developer Should Learn

  You wrote some code. You ran it. And then your array went from a list of users to an angry collection of undefined , NaN And more bugs than a summer camping trip. Staring at map , filter , and reduce like they were ancient scrolls written in Elvish. Copy-pasting from Stack Overflow like a caffeinated zombie. Wondering why the heck splice just murdered half my data. But here’s the truth: mastering arrays is non-negotiable. If you’re fumbling with arrays, you’re fumbling with everything . Web apps, APIs, UIs — they all depend on your ability to tame this glorious beast. So buckle up. I’m about to drop 25 methods that will make you look at arrays like a surgeon looks at a scalpel. Edited by me 1. map() - Because Loops Are for Cavemen You want to transform every item in an array? Don’t go forEach your way to hell. map It is concise, expressive, and doesn’t mutate your data. It’s like therapy for arrays. const names = [ 'alice' , 'bob' , 'charlie' ]; const u...

6 Essential JavaScript Concepts Every Developer Should Understand

It’s the only language I’ve used where [] == ![] it's true and where you can, typeof null and somehow get 'object' . But despite all its quirks (and there are many), there are a few core concepts that make life with JS not just easier, but saner. This isn’t some computer science flex. These are practical concepts that, once you understand them, make you write better, cleaner, and less buggy code. 1. Hoisting  Before you rage at your variables being undefined , understand this: JS hoists variable and function declarations to the top of their scope. But —  and this is important  —  only the declarations , not the assignments. Why? Because JS reads it like: This is also why let and const behave differently — they’re hoisted too, but live in the “Temporal Dead Zone” until declared. 2. Closures Closures are like little memory vaults for your functions. They allow functions to remember variables from the scope they were created in, even after that scope has gone. Why care? T...