使用ES6語法重構React組件

使用ES6語法重構React組件

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

建立組件

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

React.createClassreact

import React from 'react'; const MyComponent = React.createClass({ render: function() { return ( <div>之前的方式建立的組件</div> ); } }); export default MyComponent;

React.Component(ES6)git

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.createClasses6

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)github

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語言習慣。ide

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)ui

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)或者 箭頭函數 =>來進行手動綁定。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官方並無強制性要求使用哪一種語法,根據須要合理的選擇便可。

相關文章
相關標籤/搜索