React in Createfactory, Createclass, createelement respectively in what scenarios to use, why so defined?

Source: Internet
Author: User


Yuanyan
Links: https://www.zhihu.com/question/27602269/answer/40168594
Source: Know
Copyright belongs to the author, please contact the author for authorization.

The use of the three is slightly different, according to the Order of dependency adjustment:
1. Createclass, as its name is the class that creates the react component, describes the various behaviors you will create for the component, where the render interface of the content that needs to be output when the component is rendered must be implemented, and the others are optional:
 
 
var Hello = React.createClass({
    render: function() {
        return <div>Hello Taobao, Hello UED</div>;
    }
});

2. createelement, create react component instance, support Type,config,children three parameters:
 
ReactElement.createElement = function(type, config, children) {
  ...
}
As we described in jsx < hello/>, compiled is react.createelement (hello)

3. Createfactory, the factory method is used to create the react component instance, in JS to implement the factory method only need to create a createelement with the type parameter of the binding function:
 
ReactElement.createFactory = function(type) {
  var factory = ReactElement.createElement.bind(null, type);
  return factory;
};
The purpose of creating a pattern is to isolate and simplify the process of creating components, and the patterns of things are naturally unavailable, and if you need to create a component in bulk, you can do so through the factory method:
var h = React.createFactory(Hello);
h({x:1})
h({x:2})
h({x:3})


React Createfactory, Createclass, and createelement are used in what scenarios, why is it so defined?


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.