使用ES6語法重構React代碼


使用ES6語法重構React組件

Airbnb React/JSX Style Guide中,推薦使用ES6語法來編寫react組件。下面總結一下使用ES6 class語法創建組件和以前使用React.createClass方法來創建組件的不同。

創建組件

ES6 class創建的組件語法更加簡明,也更符合javascript。內部的方法不需要使用function關鍵字。

React.createClass

import React from 'react';

const MyComponent = React.createClass({
  render: function() {
    return (
      <div>以前的方式創建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from 'react';

class MyComponent extends Component {
  render() {
    return (
      <div>ES6方式創建的組件</div>
    );
  }
}

export default MyComponent;

props propTypes and getDefaultProps

  1. 使用React.Component創建組件,需要通過在constructor中調用super()將props傳遞給React.Component。另外react 0.13之后props必須是不可變的。
  2. 由於是用ES6 class語法創建組件,其內部只允許定義方法,而不能定義屬性,class的屬性只能定義在class之外。所以propTypes要寫在組件外部。
  3. 對於之前的getDefaultProps方法,由於props不可變,所以現在被定義為一個屬性,和propTypes一樣,要定義在class外部。

React.createClass

import React from 'react';

const MyComponent = React.createClass({
  propTypes: {
	nameProp: React.PropTypes.string
  },
  getDefaultProps() {
    return {
      nameProp: ''
    };
  },
  render: function() {
    return (
      <div>以前的方式創建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
  }
  
  render() {
    return (
      <div>ES6方式創建的組件</div>
    );
  }
}

MyComponent.propTypes = {
  nameProp: React.PropTypes.string
};
MyComponent.defaultProps = {
  nameProp: ''
};

export default MyComponent;

State

使用ES6 class語法創建組件,初始化state的工作要在constructor中完成。不需要再調用getInitialState方法。這種語法更加的符合JavaScript語言習慣。

React.createClass

import React from 'react';

const MyComponent = React.createClass({
  getInitialState: function() {
    return { data: [] };
  },
  
  render: function() {
    return (
      <div>以前的方式創建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = { data: [] };
  }
  
  render() {
    return (
      <div>ES6方式創建的組件</div>
    );
  }
}

export default MyComponent;

this

使用ES6 class語法創建組件, class中的方法不會自動將this綁定到實例中。必須使用 .bind(this)或者 箭頭函數 =>來進行手動綁定。

React.createClass

import React from 'react';

const MyComponent = React.createClass({
  handleClick: function() {
    console.log(this);
  },
  render: function() {
    return (
      <div onClick={this.handleClick}>以前的方式創建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from 'react';

class MyComponent extends Component {
  handleClick() {
    console.log(this);
  }
  
  render() {
    return (
      <div onClick={this.handleClick.bind(this)}>ES6方式創建的組件</div>
    );
  }
}

export default MyComponent;

也可以將綁定方法寫到constructor中:

import React,{ Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log(this);
  }
  
  render() {
    return (
      <div onClick={this.handleClick}>ES6方式創建的組件</div>
    );
  }
}

export default MyComponent;

或者使用箭頭函數 =>

mport React,{ Component } from 'react';

class MyComponent extends Component {
  handleClick = () => {
    console.log(this);
  }
  
  render() {
    return (
      <div onClick={this.handleClick}>ES6方式創建的組件</div>
    );
  }
}

export default MyComponent;

Mixins

使用ES6語法來創建組件是不支持React mixins的,如果一定要使用React mixins就只能使用React.createClass方法來創建組件了。

import React,{ Component } from 'react';

var SetIntervalMixin = {
  doSomething: function() {
    console.log('do somethis...');
  },
};
const Contacts = React.createClass({
  mixins: [SetIntervalMixin],
  
  handleClick() {
    this.doSomething(); //使用mixin
  },
  render() {
    return (
      <div onClick={this.handleClick}></div>
    );
  }
});

export default Contacts;

總結

總的來說使用ES6來創建組件的語法更加簡潔,這種語法避免了過多的React樣板代碼,而更多的使用純javascript語法,更符合javascript語法習慣。React官方並沒有強制性要求使用哪種語法,根據需要合理的選擇即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM