This article is synchronously connected: https://github.com/p2227/p2227.github.io/issues/3
# The pit that is easy to step on with react
## Custom component forgot to capitalize the first letter
`` `javascript
var myComp = React.createClass ({
render: function () {
return <div> Hello world </ div>;
}
});
ReactDOM.render (<myComp />, mountNode);
`` `
The above code will not render the component, because the first letter of the custom component must be capitalized
## Pass onClick and other special properties on the component
Following the above example, change myComp to MyComp, at this time it is easy to write the following code
`` `javascript
<MyComp onClick = {func} />
`` `
If there is no other processing, clicking on the component will not take effect, because all the attributes in the custom component will only be processed as a general attribute, and it needs to be processed within the component to take effect
`` `javascript
var MyComp = React.createClass ({
render: function () {
return <div onClick = {this.props.onClick}> Hello world </ div>;
}
});
ReactDOM.render (<MyComp onClick = {func} />, mountNode);
`` `
Other className, htmlFor and other attributes are also handled similarly
## Abuse this.func.bind
In the component created with `React.createClass`, the calling function of the event does not need to add .bind (this), unless you need to pass parameters
`` `javascript
var MyComp = React.createClass ({
func: function () {
// do Something
},
render: function () {
return <div onClick = {this.func.onClick}> Hello world </ div>;
}
});
`` `
If you are using ES6, you need
## Set a state directly to the input value
`` `javascript
<input value = {this.state.text} />
`` `
If you write as above, the input is unmodifiable and there are two solutions.
If you want to set a value at once, use `defaultValue`, if you want to bind in both directions, you can use` ReactLink`
defaultValue: https://facebook.github.io/react/docs/forms.html
ReactLink: http://facebook.github.io/react/docs/two-way-binding-helpers.html
React easy to tread on the first use of a hole