Make controlled component the only option?
See original GitHub issueCurrently 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:
- Created 10 years ago
- Comments:9 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Aweary nice, glad to hear it.
@jedwards1211 it should be noted that
ReactLink
is to be deprecated as of 15.0: https://github.com/facebook/react/pull/6085