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

CSS only Click-handlers You Might not be using, but you should

  You’re building a simple website, a good-looking landing page with a “See More” button. Instinctively, you reach for JavaScript to handle the button click event. But wait — what if I told you that CSS alone could do the job? Yes. CSS is often underestimated, but it can handle click interactions without JavaScript. In this guide, you’ll learn how to create CSS-only click handlers using the :target pseudo-class, and explore scenarios where this approach makes perfect sense. The :target Pseudo-Class CSS offers several pseudo-classes that let you style elements based on different states ( :hover , :focus , :checked ). But there’s one you might not have used before —  :target . The :target pseudo-class applies styles to an element when its ID matches the fragment identifier in the URL (the part after # ). This behavior is commonly seen when clicking an anchor link that jumps to a section on the same page. Here’s a simple example : <a href="#contact">Go to Contact</...

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...

The 10 Best New CSS Features in 2025 Already Supported in All Major Browsers

  CSS keeps evolving with new capabilities that make our work faster, cleaner, and more powerful. Thanks to the latest browser advances (Baseline 2024), many fresh features now work across all major engines. Below are ten highlights you can start using right away. Do you want more? Let’s check out my project, CSSToday: csstoday.dev/ 1. Scrollbar-Gutter & Scrollbar-Color When a browser displays a scrollbar, the layout can shift as space is taken up. With scrollbar-gutter , you can preserve scrollbar space even before scrolling begins: .scrollable {   scrollbar-gutter : stable both-edges; } You can also style your scrollbars with scrollbar-color : .scrollable {   scrollbar-color : #444 #ccc ; } This ensures a consistent look and prevents layout jumps. What it’s good for ✅ scrollbar-gutter keeps layouts stable by reserving space for a scrollbar, preventing annoying shifts when the scrollbar appears. scrollbar-color lets you style the scrollbar’s track and thumb, en...