Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    What is Block level Element and Inline Level Element?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor
  • JavaScript

    What is one-way data binding in React?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor
  • Frontend Interview

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor
  • Backend Interview

    What are the async and defer attributes in the “script” tag?

    By Chief Editor

    What is localization in React?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is middleware, and what is React Thunk?

    By Chief Editor

    What is Callback Hell in JavaScript?

    By Chief Editor

    What is Higher Order Component in React?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog What are controlled and uncontrolled components in React?
ReactJS

What are controlled and uncontrolled components in React?

Chief Editor
Last updated: February 16, 2025 6:15 pm
Chief Editor
Share
SHARE

When working with form inputs in React, you will often hear the terms Controlled Components and Uncontrolled Components. These refer to how the form data is handled.

Contents
1. Controlled Components:2. Uncontrolled Components:Key Differences:When to Use Each:Key Takeaways:

1. Controlled Components:

A controlled component is a component where React controls the form input elements through state.

Key Points:

  • Form data is handled by React state.
  • Input value is controlled by state, and every change updates the state.
  • You always know the current value of the input because it’s stored in state.

Example:

javascriptCopyEditimport { useState } from 'react';

function ControlledComponent() {
  const [name, setName] = useState('');

  const handleChange = (e) => {
    setName(e.target.value);
  };

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

  return (
    <form onSubmit={handleSubmit}>
      <input type="text" value={name} onChange={handleChange} />
      <button type="submit">Submit</button>
    </form>
  );
}

How It Works:

  • value={name} → The input’s value is set by state.
  • onChange={handleChange} → Updates state whenever the input value changes.

Advantages:

  • Easy to manage and validate input values.
  • Can manipulate input values programmatically.
  • Recommended in React as the standard approach.

2. Uncontrolled Components:

An uncontrolled component is a component where the form input is handled by the DOM itself, not React state.

Key Points:

  • Form data is handled by the DOM, not React state.
  • Access the value using ref when needed.
  • React is not aware of the input’s current value until you manually read it.

Example:

javascriptCopyEditimport { useRef } from 'react';

function UncontrolledComponent() {
  const inputRef = useRef(null);

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

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

How It Works:

  • ref={inputRef} → Creates a reference to the input element.
  • Access value with inputRef.current.value when needed (e.g., on form submission).

Advantages:

  • Useful when integrating with non-React libraries.
  • Can be simpler for non-interactive inputs (e.g., forms that don’t need real-time validation).

Key Differences:

FeatureControlled ComponentsUncontrolled Components
Value HandlingControlled by React state.Controlled by the DOM.
Input Value AccessFrom state.Using ref.
Real-time UpdatesYes, re-rendered on every change.No, React doesn’t track input value.
ValidationEasier (using state).Requires manual handling.
Recommended?Yes, preferred in React applications.Sometimes, for specific cases like file uploads or third-party libraries.

When to Use Each:

Use CaseBest Choice
Form with validation, dynamic updatesControlled Component.
Simple form, no state neededUncontrolled Component.
Third-party libraries, file inputsUncontrolled Component.

Key Takeaways:

  • Controlled Components → React state controls input.
  • Uncontrolled Components → DOM controls input; React accesses it via ref.
  • Controlled is generally preferred in React, except for special cases like file uploads.

Let me know if you need more examples or help with React concepts! 😊🚀

Share This Article
Email Copy Link Print
Previous Article How Does React Work?
Next Article What is a Function Component in React?
Leave a Comment

Leave a Reply Cancel reply

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

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

Difference between display none and visibility hidden in CSS?

The display: none; and visibility: hidden; properties in CSS both make elements invisible, but they…

By Chief Editor

How Can You Share Data Between Components in React?

In React, there are several ways to share data between components, depending on the relationship…

By Chief Editor

What is memoization in JavaScript?

Memoization is an optimization technique used in JavaScript (and other programming languages) to speed up…

By Chief Editor

You Might Also Like

ReactJS

What is Virtual DOM in React?

By Chief Editor
ReactJS

Explain the uesReducer and useContext hooks in React

By Chief Editor
How-to-Improve-the-Performance-of-React-Applications
React InterviewReactJS

How to Improve the Performance of React Applications

By Chief Editor
ReactJSRedux

How can you delay the dispatch in React?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?