使用ES6語法重構React代碼詳解
使用ES6語法重構React組件
在Airbnb React/JSX Style Guide中,推薦使用ES6語法來編寫react組件。下面總結一下使用ES6 class語法創(chuàng)建組件和以前使用React.createClass方法來創(chuàng)建組件的不同。
創(chuàng)建組件
ES6 class創(chuàng)建的組件語法更加簡明,也更符合javascript。內(nèi)部的方法不需要使用function關鍵字。
React.createClass
import React from 'react'; const MyComponent = React.createClass({ render: function() { return ( <div>以前的方式創(chuàng)建的組件</div> ); } }); export default MyComponent;
React.Component(ES6)
import React,{ Component } from 'react'; class MyComponent extends Component { render() { return ( <div>ES6方式創(chuàng)建的組件</div> ); } } export default MyComponent;
props propTypes and getDefaultProps
1.使用React.Component創(chuàng)建組件,需要通過在constructor中調(diào)用super()將props
傳遞給React.Component。另外react 0.13之后props
必須是不可變的。
2.由于是用ES6 class語法創(chuàng)建組件,其內(nèi)部只允許定義方法,而不能定義屬性,class的屬性只能定義在class之外。所以propTypes
要寫在組件外部。
3.對于之前的getDefaultProps
方法,由于props不可變,所以現(xiàn)在被定義為一個屬性,和propTypes
一樣,要定義在class外部。
React.createClass
import React from 'react'; const MyComponent = React.createClass({ propTypes: { nameProp: React.PropTypes.string }, getDefaultProps() { return { nameProp: '' }; }, render: function() { return ( <div>以前的方式創(chuàng)建的組件</div> ); } }); export default MyComponent;
React.Component(ES6)
import React,{ Component } from 'react'; class MyComponent extends Component { constructor(props) { super(props); } render() { return ( <div>ES6方式創(chuàng)建的組件</div> ); } } MyComponent.propTypes = { nameProp: React.PropTypes.string }; MyComponent.defaultProps = { nameProp: '' }; export default MyComponent;
State
使用ES6 class語法創(chuàng)建組件,初始化state的工作要在constructor中完成。不需要再調(diào)用getInitialState
方法。這種語法更加的符合JavaScript語言習慣。
React.createClass
import React from 'react'; const MyComponent = React.createClass({ getInitialState: function() { return { data: [] }; }, render: function() { return ( <div>以前的方式創(chuàng)建的組件</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方式創(chuàng)建的組件</div> ); } } export default MyComponent;
this
使用ES6 class語法創(chuàng)建組件, class中的方法不會自動將this綁定到實例中。必須使用 .bind(this)
或者 箭頭函數(shù) =>
來進行手動綁定。
React.createClass
import React from 'react'; const MyComponent = React.createClass({ handleClick: function() { console.log(this); }, render: function() { return ( <div onClick={this.handleClick}>以前的方式創(chuàng)建的組件</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方式創(chuàng)建的組件</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方式創(chuàng)建的組件</div> ); } } export default MyComponent;
或者使用箭頭函數(shù) =>
:
import React,{ Component } from 'react'; class MyComponent extends Component { handleClick = () => { console.log(this); } render() { return ( <div onClick={this.handleClick}>ES6方式創(chuàng)建的組件</div> ); } } export default MyComponent;
Mixins
使用ES6語法來創(chuàng)建組件是不支持React mixins的,如果一定要使用React mixins就只能使用React.createClass方法來創(chuàng)建組件了。
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來創(chuàng)建組件的語法更加簡潔,這種語法避免了過多的React樣板代碼,而更多的使用純javascript語法,更符合javascript語法習慣。React官方并沒有強制性要求使用哪種語法,根據(jù)需要合理的選擇即可。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
react 報錯Module build failed: Browserslis
這篇文章主要介紹了react 報錯Module build failed: BrowserslistError: Unknown browser query `dead`問題的解決方法,需要的朋友可以參考下2023-06-06React-Native之截圖組件react-native-view-shot的介紹與使用小結
這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結,需本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,要的朋友可以參考下2021-08-08