版本显示:React-Native -v
react-native-cli:0.2.0
react-native:0.22.2
关于props
state
问题1:
在ES6
语言风格下,使用如下方式定义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
的风格下以上是不被推荐的,而是换成如下方式:
- 定义
props
:
static defaultProps={
name:'xxxx',
age:10
}
- 定义
state
:
则在是constructor
构造方法中定义相关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:'深圳南山区'};
}
关于state
props
的含义与区别可以点这
网友评论