2017-08-28 24 views
0

私は2つの画面を持っています。 最初の画面はHomeScreen、2番目の画面はProfileScreenです。 私はHomeScreenでFlatListを使用しました。私は別の画面に移動するためにプッシュしたいと思います。あなたが実装にthisのコンテキストを失っていること画面の変更FlatList onPress

class ProfileScreen extends Component { 

    static navigationOptions = { 
     title: 'Profile', 
    }; 

    render() { 
     const { navigate } = props.navigation; 

     return <Text>Hello, I am profile!</Text>; 
    } 
} 




class HomeScreen extends Component { 

    static navigationOptions = { 
     title: 'Home', 
    }; 


    constructor(props) { 
     super(props); 

     this.state = { 
      data: [], 

     }; 


    } 

    getScreen() { 
     this.props.navigation.navigate('Profile') 
    } 

    render() { 

     return (
      <View> 
       <FlatList 
        data={this.state.data} 
        renderItem={({ item }) => (
         <TouchableHighlight underlayColor= 'transparent' onPress= {this.getScreen}> 
          <View style= {{width: 300, height: 'auto'}} > 
          <Text> {item.title} </Text> 
           <View style= {{width: 300, height: 1, backgroundColor: 'red', marginBottom: 30, marginTop: 15}} /> 
          </View> 
         </TouchableHighlight> 
        )} 
       /> 
      </View> 
     ); 
    } 
} 



const AppNavigator = StackNavigator({ 
    Home: { screen: HomeScreen }, 
    Profile: { screen: ProfileScreen } 
}); 
+0

this.props.navigationを使用して、この行を最後に追加してください。デフォルトのAppNavigatorをエクスポートします –

+0

私は理解できませんでした、あなたはそれを説明できますか? – tersintersi

+0

change const {navigation} = this.props.navigation –

答えて

0

よう

コード「はundefinedプロパティを読むと 『移動』することはできません」:私はcodesことを使用whenしかし、私はそのエラーメッセージを見ました。関数呼び出しでそれを修正:あなたのgetScreenメソッドにこれをバインドするコンストラクタの内部

const { navigate } = this.props.navigation; 

navigate('Profile'); 
+0

それは理由ではないかもしれません、私は同じ問題を満たして、それは動作しません – hanzichi

0

:また

renderItem={({ item }) => (
    <TouchableHighlight underlayColor='transparent' onPress={() => this.getScreen()}> 
    ... 
    </TouchableHighlight> 
)} 

、あなたはパターンを使用することができます。

コンストラクタ内に次の行を追加するだけです。

this.getScreen = this.getScreen.bind(this);

関連する問題