版本顯示:React-Native -v
react-native-cli:0.2.0
react-native:0.22.2
關(guān)于props
state
問題1:
在ES6
語言風(fēng)格下,使用如下方式定義props
與state
提示如下:
getInitialState(){}
getDefaultProps() {}
Waring:getDefaultProps was defined on xxx,a plain javascript class. This is only supported for classes created using React.createClass.Use a static property to define defaultProps instead.
也就是說刀荒,在ES6
的風(fēng)格下以上是不被推薦的,而是換成如下方式:
- 定義
props
:
static defaultProps={
name:'xxxx',
age:10
}
- 定義
state
:
則在是constructor
構(gòu)造方法中定義相關(guān)state
constructor(props) {
super(props);
this.state = {name:'xxx'};
}
完整如下 :
ES6 Classes: The API is similar to React.createClass with the exception of getInitialState. Instead of providing a separate getInitialState method, you set up your own state property in the constructor.
Another difference is that propTypes and defaultProps are defined as properties on the constructor instead of in the class body.
static defaultProps={
age:12,
name:'xxx'
}
constructor(props) {
super(props);
this.state = {address:'深圳南山區(qū)'};
}
關(guān)于state
props
的含義與區(qū)別可以點(diǎn)這