我有兩個屏幕。第一個屏幕是HomeScreen,第二個屏幕是ProfileScreen。 我在HomeScreen上使用了FlatList,我想推導航到另一個屏幕。但是,當我使用的代碼,我看到錯誤消息:「不能讀取屬性‘導航’的未定義」更改屏幕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 }
});
嘗試使用this.props.navigation並在最後添加此行導出默認AppNavigator –
我不明白,你能解釋一下嗎? – tersintersi
更改常量{導航} = this.props.navigation –