Controlled vs Uncontrolled components


Help me to understand what and where is the benefit of using uncontrolled components ( "input" tags for example )?
Why do I have to add ''value={this.state.userInput}'' on an uncontrolled component and make it controlled that way?
If I remove that chunk of code ''value={this.state.userInput}'' the program still works.
What did I miss ?

var React = require('react');
var ReactDOM = require('react-dom');

var Input = React.createClass({
  getInitialState: function(){
      userInput: ''
  handleUserInput: function (e) {
  render: function () {
    return (
        <input type="text" onChange={this.handleUserInput}  />

	<Input />,


There's only one problem: you can set this.state.userInput to whatever you want, but won't care. You need to somehow make the 's text responsive to this.state.userInput.
Easy enough! You can control an 's text by setting it's value attribute.

<input type="text" onChange={this.handleUserInput} value={this.state.userInput} />

In this example, we are accepting the value provided by the user and updating the value prop of the component.

Sitepoint video: Introduction to One-Way Data Flow

Sitepoint video: Controlled Components vs. Uncontrolled Components in React.js


Thank you for the answer.
The second Sitepoint video helped me a lot. :slight_smile:


You're welcome. I figured it would.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.