Friday, 18 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • 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

    What are the different types of HTML tags?

    By Chief Editor

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

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

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

    By Chief Editor
  • JavaScript

    What is a Closure in JavaScript?

    By Chief Editor

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    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
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
  • Backend Interview

    What is a Higher-Order Component (HOC) in React?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What is a Promise in JavaScript, and what are its parameters?

    By Chief Editor

    What is state in React, and what are its properties?

    By Chief Editor

    What are controlled and uncontrolled components in React?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home 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 document.createElement and document.createElementFragement in JavaScript?

Difference Between document.createElement() and document.createDocumentFragment() in JavaScript 1. document.createElement() This method creates a single HTML…

By Chief Editor

How Does React Work?

React is a JavaScript library used for building user interfaces, particularly for single-page applications (SPAs).It…

By Chief Editor

What is Arrow and Normal Function in JavaScript?

Arrow Function vs Normal Function in JavaScript In JavaScript, Arrow Functions and Normal Functions (also…

By Chief Editor

You Might Also Like

ReactJSRedux

What is middleware, and what is React Thunk?

By Chief Editor
Mastering Star Rating Systems: Best Practices and Optimized Code Examples
ReactJSReact Interview

Mastering Star Rating Systems: Best Practices and Optimized Code Examples

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

How to Improve the Performance of React Applications

By Chief Editor
ReactJS

Explain the uesReducer and useContext hooks in React

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?