null 不是一个对象(评估 this.state.count)
null is not an object(evaluating this.state.count)
当我尝试为每次按下按钮设置计数时,我遇到了上述错误。
export default class ViewIdeas extends Component{
get InitialState(){
return {
count : 0
}
}
render(){
return(
.......
<Button transparent>
<Icon name='ios-thumbs-up-outline' style={{fontSize:21}}
onPress={() => this.setState({count: ++this.state.count})}/>
<Text>{'Like' + this.state.count}</Text>
</Button>
请这样设置状态
constructor(props){
super(props);
this.state = {
count: 0,
}
}
然后你可以this.state.count
有两种制作 React class 的方法:ES6 和使用 React.createClass.
这两种方法不可互换。使用 ES6 classes 时应在构造函数中初始化状态,使用 React.createClass
.
时定义 getInitialState
方法
See the official React doc on the subject of ES6 classes.
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { /* initial state */ };
} // Note that there is no comma after the method completion
}
等同于
var MyComponent = React.createClass({
getInitialState() {
return { /* initial state */ };
},
});
还需要注意的一件事是构造方法后没有逗号。
当我尝试为每次按下按钮设置计数时,我遇到了上述错误。
export default class ViewIdeas extends Component{
get InitialState(){
return {
count : 0
}
}
render(){
return(
.......
<Button transparent>
<Icon name='ios-thumbs-up-outline' style={{fontSize:21}}
onPress={() => this.setState({count: ++this.state.count})}/>
<Text>{'Like' + this.state.count}</Text>
</Button>
请这样设置状态
constructor(props){
super(props);
this.state = {
count: 0,
}
}
然后你可以this.state.count
有两种制作 React class 的方法:ES6 和使用 React.createClass.
这两种方法不可互换。使用 ES6 classes 时应在构造函数中初始化状态,使用 React.createClass
.
getInitialState
方法
See the official React doc on the subject of ES6 classes.
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { /* initial state */ };
} // Note that there is no comma after the method completion
}
等同于
var MyComponent = React.createClass({
getInitialState() {
return { /* initial state */ };
},
});
还需要注意的一件事是构造方法后没有逗号。