2017-09-27 15 views
0

反応ナビゲーションを使用して別の画面にナビゲートしようとしています。しかし、このコードで私はエラーが発生します。undefined is not an object (evaluation 'this.props.navigation')別のメソッド内でnavigateを呼び出せません

navigateを直接呼び出すと(下の例を参照)、それは機能します。

私は間違っていますか?

これは動作しません:あなたはコンテキストをバインドする必要が

_renderRow (rowData) { 
    return (
     <View > 
     <TouchableHighlight onPress={() => this.props.navigation.navigate('Details')} underlayColor='white'> 
      <Image 
       style={{width: 50, height: 50}} 
       source={{uri: 'http://openweathermap.org/img/w/' + rowData.iconName + '.png'}} 
      /> 
     </TouchableHighlight> 
     </View> 
    ) 
    } 
+1

'たonPress = {this._handlePress.bind(本)}' – Gerardo

答えて

1

_renderRow (rowData) { 
    return (
     <View > 
     <TouchableHighlight onPress={this._handlePress} underlayColor='white'> 
      <Image 
       style={{width: 50, height: 50}} 
       source={{uri: 'http://openweathermap.org/img/w/' + rowData.iconName + '.png'}} 
      /> 
     </TouchableHighlight> 
     </View> 
    ) 
    } 

    _handlePress() { 
    this.props.navigation.navigate('Details') 
    } 

これは動作しません。 onPress={this._handlePress.bind(this)}が作業を行います。ここ

You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick , this will be undefined when the function is actually called.

は、ドキュメントの:

Handling events

関連する問題