A pit that is easy to tread with react

Source: Internet
Author: User


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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.