null不是对象(评估this.state.count)

Avi*_*ati 13 react-native react-native-android

当我试图为每次按下按钮时设置计数时,我面临上述错误.

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>
Run Code Online (Sandbox Code Playgroud)

Suh*_*hta 19

请设置这样的状态

constructor(props){
   super(props);

   this.state = {
      count: 0,
   }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以做this.state.count