question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make controlled component the only option?

See original GitHub issue

Currently the only advantage I can think of, of using uncontrolled components, is for their lightweight syntax. But ReactLink made me wonder if there isn’t a better way.

Currently:

var Uncontrolled = React.createClass({
  render: function() {
    return <input type="text" defaultValue="hello" />;
  }
});
var Controlled = React.createClass({
  getInitialState: function() {
    return {text: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({text: event.target.value});
  },
  render: function() {
    return <input type="text" value={this.state. text} onChange={this.handleChange} />;
  }
});

My proposal: Keep controlled the same. Controlled with sugar:

var Controlled = React.createClass({
  render: function() {
    return <input type="text" value="hello" stateLink="text" />;
  }
});

I realize this is a bit of magic, but considering that currently we need to:

  • Explain what defaultValue is and its reason for deviating from DOM.
  • Provide an add-on (in another build) to have the benefits of uncontrolled components, which might seem a bit backward.
  • Have value anyway.

I feel the pros outweigh the cons here. With the new format you still have two options very similar to controlled/uncontrolled, but with all the benefits of a controlled component.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jedwards1211commented, Mar 31, 2016

@Aweary nice, glad to hear it.

1reaction
awearycommented, Mar 30, 2016

@jedwards1211 it should be noted that ReactLink is to be deprecated as of 15.0: https://github.com/facebook/react/pull/6085

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncontrolled Components - React
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by...
Read more >
Controlled vs Uncontrolled Components in React - ITNEXT
Uncontrolled components store their data in the DOM like a traditional HTML input element. React.
Read more >
React Controlled Components, the Hooks Way - Dmitri Pavlutin
The step by step guide on how to implement controlled components in React using hooks.
Read more >
Controlled vs. uncontrolled components in React
In this tutorial, we'll explain the difference between controlled and uncontrolled components in React with practical examples.
Read more >
What are React controlled components and uncontrolled ...
We create a controlled component by connecting the form element ( <input> , <textarea> or <select> ) to the state by setting its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found