加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React.createClass versus extends React.Component

发布时间:2020-12-15 03:25:01 所属栏目:百科 来源:网络整理
导读:React.createClass versus extends React.Component Posted on Jan 4,2016-Edit this page on GitHub 90% Unlimited Downloads Choose from Over 300,000 Vectors,Graphics Photos. ads viaCarbon Two ways to do the same thing. Almost. React traditional

React.createClass versus extends React.Component

90% Unlimited Downloads Choose from Over 300,000 Vectors,Graphics & Photos.ads viaCarbon

Two ways to do the same thing. Almost. React traditionally provided theReact.createClassmethod to create component classes,and released a small syntax sugar update to allow for better use with ES6 modules byextends React.Component,which extends theComponentclass instead of callingcreateClass.

These differences are subtle in places,but have quite a few interesting differences worth exploring,which will allow you to make the best decision for which is best for you.

Syntax differences

First,let’s explore the syntax differences by looking at two code examples and annotating them.

React.createClass

Here we have aconstwith a React class assigned,with the importantrenderfunction following on to complete a typical base component definition.

import React from 'react';

const Contacts = React.createClass({
  render() {
    return (
      <div></div>     );
  }
});

export default Contacts;
React.Component

Let’s take the aboveReact.createClassdefinition and convert it to use an ES6 class.

class Contacts extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    ></div>     );
  }
}

default Contacts;

From a JavaScript perspective we’re now using ES6 classes,typically this would be used with something like Babel to compile the ES6 to ES5 to work in other browsers. With this change,we introduce theconstructor,where we need to callsuper()to pass the props toReact.Component.

For the React changes,we now create aclasscalled “Contacts” andextendfromReact.Componentinstead of accessingReact.createClassdirectly,which uses less React boilerplate and more JavaScript. This is an important change to note further changes this syntax swap brings.

propTypes and getDefaultProps

There are important changes in how we use and declare default props,their types and setting initial states,let’s take a look.

React.createClass

In theReact.createClassversion,thepropTypesproperty is an Object in which we can declare the type for each prop. ThegetDefaultPropsproperty is a function that returns an Object to create initial props.

= React.createClass({
  propTypes: {

  },
  getDefaultProps() {
    return {
      
    };
  },
  render() {
    default Contacts;
React.Component

This usespropTypesas a property on the actualContactsclass instead of a property as part of thecreateClassdefinition Object. I think it’s nicer syntax to create class properties so it’s much clearer what are React APIs versus your own on the definition Object.

ThegetDefaultPropshas now changed to just an Object property on the class calleddefaultProps,as it’s no longer a “get” function,it’s just an Object. I like this syntax as it avoids more React boilerplate,just plain JavaScript.

></div>     );
  }
}
Contacts.propTypes = {

};
Contacts.defaultProps = {

};

default Contacts;

State differences

State is an interesting change,now we’re using constructors the implementation of initial states changes.

React.createClass

We have agetInitialStatefunction,which simply returns an Object of initial states.

= React.createClass({
  getInitialState () {
    default Contacts;
React.Component

ThegetInitialStatefunction is deceased,and now we declare all state as a simple initialisation property in thesuper(props); this.state = { }; } render() { default Contacts;

“this” differences

UsingReact.createClasswill automatically bindthisvalues correctly for us,but changes when using ES6 classes affect this.

React.createClass

Note theonClickdeclaration withthis.handleClickbound. When this method gets called React will apply the right execution context tohandleClick.

= React.createClass({
  handleClick() {
    console.log(this); // React Component instance
  },170)"><div onClick={this.handleClick}default Contacts;
React.Component

With ES6 classes this is slightly different,properties of the class do not automatically bind to the React class instance.

super(props);
  }
  handleClick() {
    console.log(// null
  }
  render() {
    default Contacts;

There are a few ways we could bind the right context,here’s how we could bind inline:

// React Component instance
  }
  render() {
    this.handleClick.bind(this)}default Contacts;

Alternatively we could change the context ofthis.handleClickinside theconstructorto avoid inline repetition,which may be a better approach if moving to this syntax to avoid touching JSX at all:

this.handleClick = this);
  }
  handleClick() {
    console.log(default Contacts;

Mixins

React mixins are no longer supported when using React components written in ES6.

React.createClass

WithReact.createClasswe can add mixins to components using amixinsproperty which takes an Array of available mixins. These then extend the component class.

'react';

var SomeMixin = {
  doSomething() {

  }
};
= React.createClass({
  mixins: [SomeMixin],
  handleClick() {
    this.doSomething(); // use mixin
  },254)">default Contacts;
React.Component

Mixins aren’t supported in ES6 classes.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读