代码在App.js中正常运行,但在About.js中不正常
我尝试了在google中找到的所有内容,但始终给我“无法读取未定义的属性'navigate'。
我该如何解决?请帮我解决这个问题。您的帮助将不胜感激,如果需要更多信息,请让我知道
App.js
const AppNavigator = createStackNavigator({
Home: {
screen: HomeScreen,
},
Details: {
screen: DetailsScreen,
},
Audios: {
screen: FouraudioScreen,
},
About: {
screen: AboutScreen,
navigationOptions : {
header: null
}
},
Gallery: {
screen: GalleryScreen,
navigationOptions : {
header: null
}
},
}, {
initialRouteName: 'Home',
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
color:'#1c2747',
// alignItems: 'center',
//justifyContent: 'center',
},
});
const AppContainer = createAppContainer(AppNavigator);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
// About.js
render() {
return (
<View>
<Button
title="Go to Gallery"
onPress={() => this.props.navigation.navigate('Gallery')}
/>
</View>
)
}
const RootStack = createStackNavigator(
{
About: {
screen: AboutScreen,
},
Gallery: {
screen: GalleryScreen,
},
},
{
headerMode: 'none',
}
);
const AboutContainer = createAppContainer(RootStack);
class About extends React.Component {
render() {
return <AboutScreen />;
}
};
export default About;
添加import { withNavigation } from 'react-navigation';
到About.js并将该类导出为export default withNavigation(About.js)