React在ES6的實現(xiàn)中去掉了getInitialState和getDefaultProps
1、React在ES6的實現(xiàn)中去掉了getInitialState這個hook函數(shù),規(guī)定state在constructor中實現(xiàn),寫法如下:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
}
2、寫default props有兩種方法
//1 在組件內(nèi)部的使用static
static defaultProps = {
name: ...
}
//2 在組件外部
Hello.defaultProps = {
name: ...
}
React在ES5中默認的 state和props的方法
var MyComponent = React.createClass({
getDefaultProps(){
return{
name: ...
}
},
getInitialState(){
return{
name: ...
}
},
});