[英]undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native
I am trying to make my first React Native Android app and I am getting this error:我正在尝试制作我的第一个 React Native Android 应用程序,但出现此错误:
undefined is not an object (evaluating 'this.props.navigation.navigate')
undefined 不是一个对象(评估'this.props.navigation.navigate')
This is the code:这是代码:
import React from 'react';
import { StyleSheet, Text, View, Button, TextInput } from 'react-native';
import { StackNavigator } from 'react-navigation';
export default class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Button
title="Show Centers near me"
onPress={() =>
navigate('Results', "Search Term")
}
/>
<Text>or</Text>
</View>
);
}
}
class ResultsScreen extends React.Component {
static navigationOptions = {
title: 'Results',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hi</Text>
</View>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Results: { screen: ResultsScreen }
});
I can not figure out why the error is coming.我无法弄清楚为什么会出现错误。
You are exporting the component wrong.您正在错误地导出组件。 You should get rid of the
export default
on your class HomeScreen
definition and at the bottom of the file do export default App;
你应该在你的
class HomeScreen
定义中去掉export default
,并在文件底部做export default App;
If you are handling it in AppContainer and not able to access while opening drawer menu.如果您在 AppContainer 中处理它并且无法在打开抽屉菜单时访问它。 You can try below snippets.
您可以尝试以下代码段。
const HomeStackNavigator = createStackNavigator({ Home: {
screen: Home,
navigationOptions : ({navigation}) => ({
title: 'Home',
headerStyle: {
backgroundColor: "#512DA8"
},
headerTitleStyle: {
color: "#fff"
},
headerTintColor: "#fff",
headerLeft: <TouchableOpacity onPress={ () => navigation.openDrawer()}>
<Image
source={require('./images/menu_burger.png')}
style={{width: 24, height: 24, padding: 5, marginLeft: 5}}/>
</TouchableOpacity>
}) } }, { initialRouteName: 'DumontHome' })
尝试不使用“搜索词”或以这种方式:
navigate('route', {item:'search term'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.