利用ES6語法重構(gòu)React組件詳解
一、創(chuàng)建組件
ES6 class創(chuàng)建的組件語法更加簡明,也更符合javascript。內(nèi)部的方法不需要使用function關(guān)鍵字。
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
. 使用React.Component
創(chuàng)建組件,需要通過在constructor中調(diào)用super()
將props傳遞給React.Component
。另外react 0.13之后props必須是不可變的。
. 由于是用ES6 class語法創(chuàng)建組件,其內(nèi)部只允許定義方法,而不能定義屬性,class的屬性只能定義在class之外。所以propTypes要寫在組件外部。
. 對(duì)于之前的getDefaultProps方法,由于props不可變,所以現(xiàn)在被定義為一個(gè)屬性,和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;
三、狀態(tài)
. 使用ES6 class語法創(chuàng)建組件,初始化state的工作要在constructor中完成。不需要再調(diào)用getInitialState方法。這種語法更加的符合JavaScript語言習(xí)慣。
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中的方法不會(huì)自動(dòng)將this綁定到實(shí)例中。必須使用 .bind(this)
或者 箭頭函數(shù) =>來進(jìn)行手動(dòng)綁定。
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;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,如果有疑問大家可以留言交流。
相關(guān)文章
react使用axios實(shí)現(xiàn)上傳下載功能
這篇文章主要為大家詳細(xì)介紹了react使用axios實(shí)現(xiàn)上傳下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08React中使用react-json-view展示JSON數(shù)據(jù)的操作方法
react-json-view是一個(gè)用于顯示和編輯javascript數(shù)組和JSON對(duì)象的React組件,本文給大家分享React中使用react-json-view展示JSON數(shù)據(jù)的操作方法,感興趣的朋友一起看看吧2023-12-12一文教會(huì)你用redux實(shí)現(xiàn)computed計(jì)算屬性
在computed中,可以定義一些屬性,即計(jì)算屬性,下面這篇文章主要給大家介紹了關(guān)于如何利用redux實(shí)現(xiàn)computed計(jì)算屬性的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05