利用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要寫在組件外部。
. 對于之前的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;
三、狀態(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中的方法不會自動將this綁定到實例中。必須使用 .bind(this)或者 箭頭函數(shù) =>來進(jìn)行手動綁定。
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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,如果有疑問大家可以留言交流。
相關(guān)文章
react-redux action傳參及多個state處理的實現(xiàn)
本文主要介紹了react-redux action傳參及多個state處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
React動態(tài)更改html標(biāo)簽的實現(xiàn)方式
這篇文章主要介紹了React動態(tài)更改html標(biāo)簽的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
react中關(guān)于函數(shù)調(diào)用()與bind this的原因及分析
這篇文章主要介紹了react中關(guān)于函數(shù)調(diào)用()與bind this的原因及分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
react中form.setFieldvalue數(shù)據(jù)回填時 value和text不對應(yīng)的問題及解決方法
這篇文章主要介紹了react中form.setFieldvalue數(shù)據(jù)回填時 value和text不對應(yīng)的問題及解決方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
react?express實現(xiàn)webssh?demo解析
這篇文章主要為大家介紹了詳解react?express實現(xiàn)webssh?demo解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

