2017-04-03 22 views
3

ボタンを押すたびにカウントを設定しようとすると、上記のエラーに直面しています。nullはオブジェクトではありません(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> 

答えて

5

この

constructor(props){ 
    super(props); 

    this.state = { 
     count: 0, 
    } 
} 

のような状態に設定してください。そして、あなたが行うことができますthis.state.count

0

2クラス反応させることのアプローチがあります:ES6とReact.createClassを使用しては、 。

2つのアプローチは互換性がありません。 ES6クラスを使用する場合はコンストラクタで状態を初期化し、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 */ }; 
    }, 
}); 

もう一つのことに相当します。

関連する問題