Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Controlled vs Uncontrolled Components
Katelyn
Katelyn

Posted on

     

Controlled vs Uncontrolled Components

What is the difference between controlled and uncontrolled components in react?

To put simply, controlled components have their data being handled with a react component whereas an uncontrolled component's data is being handled with the DOM itself. Let's dig into this answer a bit more though starting at the basics and including a few visual examples.

What are controlled and uncontrolled components?

Form elements are rendered with HTML within React components where data is being accessed and manipulate.When we are discussing uncontrolled and controlled components these are terms that are specifically discussing the way in which the form created is handling and accessing that said data. The data handling can be done a few different ways but is commonly seen using typed elements like,<input> and<textarea> or selected elements such as:<checkbox>, <select>, <radiobutton>.

Visual chart with differences between controlled and uncontrolled components

Controlled Components

As we stated earlier controlled components handle their updated data using use state. This would look like setting the value for the input form element tothis.state.value or to a use state. When setting these element's value to use state, we have wrapped up the control for both the rendering of the form as well as future input of the form into the same React component. Another way to think of is is that the React state will always act as "the source of truth". As users interact with the form, handleChange will run on every keystroke or interaction - which then updates the React state.

The React documentation acknowledges that writing out controlled components can feel banal since you do need to create an event handler for each way the data can change while also containing that in the React component use state.

controlled vs uncontrolled forms

Uncontrolled Components

A helpful tidbit to remember about uncontrolled components is that the part of the reason it's uncontrolled is because the value is set by the user and not by the program. With this in mind the input:<input type="file" /> will always be uncontrolled without the value being set. This will render the for element's, where the form element's data is handled by the DOM. In this way it functions similarly to traditional HTML code.

Due to uncontrolled components keeping their "source of truth" in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components.

controll vs uncontroll

TL;DR

kyle's version

The primary difference between a controlled component vs an uncontrolled component is related to how they handle their value. Uncontrolled components pass down the value through props. In contrast, controlled components use state to handle the value internally. For most use cases, controlled components are the best option in code.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
andrewsjoey17 profile image
Andrews Joey
Hi, I’m Andrews Joey, a passionate software developer with 9 years of experience in software development. I specialize in programming languages like JavaScript, Python, React, Node.js, and Angular.
  • Location
    Ottawa, Ontario
  • Education
    University of Ottawa
  • Pronouns
    Andrews
  • Work
    Technical lead at Nokia, Ottawa
  • Joined

In controlled components, the form input’s value is fully handled by React state.
uncontrolled components rely on the DOM itself to manage the input state.
Many apps use a mix depending on the complexity of each form or input. Really grateful content.

CollapseExpand
 
un4uthorized profile image
Miguel
you’re the biggest tool.
  • Location
    Brazil
  • Education
    Computer Science - Pontifical Catholic University Of Minas Gerais
  • Work
    Software Engineer
  • Joined

Thanks for sharing, it was very clear!

CollapseExpand
 
tanth1993 profile image
tanth1993
  • Joined

now I know more aboutcontrolled and uncontrolled components. in most cases, I prefercontrolled components because they can be controlled easily.

CollapseExpand
 
amandaolsen profile image
amandaolsen
UI Engineer
  • Joined

Referencing your image with the black background above, a controlled component's "value" attribute would not be "Hey Dude!" Instead it would call state or props.
Hope that's helpful!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

|| Full Stack Developer ||
  • Location
    Seattle, WA
  • Education
    B.A. of Education from Faith Int University
  • Joined

More fromKatelyn

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp