react-native - Uncaught Error: undefined is not an object (evaluating 't.props.navigation.navigate') touchableHandlePress@[native code]
问题描述
I am creating a component called HomeIcon
and inserting it in my header
via defaultNavigationOptions
> headerRight
, I added an onPress
in this component by assigning this.props.navigation.navigate('Main');
with the purpose that clicking on this component loads the MainScreen
but when I click the error described in the above heading occurs.
Here's the code:
import React from 'react';
import { StyleSheet, TouchableOpacity, Image, Dimensions } from 'react-native';
export default class HomeIcon extends React.Component {
render() {
return (
<TouchableOpacity onPress={() => {
this.props.navigation.navigate('Main');
}}>
<Image style={styles.buttonHome} source={require('../icons/home2.png')} />
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
buttonHome: {
aspectRatio: 1,
resizeMode: 'contain',
height: Dimensions.get('window').width * 0.08,
width: Dimensions.get('window').height * 0.08,
margin: Dimensions.get('window').height * 0.018
}
});
import React from 'react';
import { createAppContainer, createStackNavigator } from 'react-navigation';
import Main from './source/screens/MainScreen';
import CustomCards from './source/screens/CustomCardsScreen';
import HomeIcon from './source/components/HomeIcon';
const AppNavigator = createStackNavigator ({
'Main': {
screen: Main,
navigationOptions: {
title: 'Tela Principal'
}
},
'CustomCards': {
screen: CustomCards,
navigationOptions: {
title: 'Cartões Personalizados'
}
}
}, {
defaultNavigationOptions: {
headerTitleStyle: {
flexGrow: 1,
fontWeight: 'bold',
textAlign: 'center'
},
headerLeft: (null),
headerRight: (
<HomeIcon />
),
headerStyle:{
backgroundColor: '#7d253b'
},
headerTintColor: '#FFF'
}
});
const AppContainer = createAppContainer(AppNavigator);
export default AppContainer;
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject"
},
"dependencies": {
"@types/react": "^16.8.13",
"@types/react-native": "^0.57.43",
"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-navigation": "^3.6.1"
},
"devDependencies": {
"babel-preset-expo": "^5.0.0"
},
"private": true
}
Visit the project repository in GitHub for more details: https://github.com/Alex-Xavier/ACTIFICA
解决方案
You need to pass a navigation object to that component. 'this.props.navigation' only available in a screens that is directly assign to stack/tab/drawer navigation. In your case it's screens 'Main' and 'CustomCards'
HomeIcon component have no idea what is a this.props.navigation.
You have to pass 'navigation' as a props. Like so:
headerRight: ({ navigation }) => <HomeIcon navigation={navigation}/>
Then in you HomeIcon it's will be available as this.props.navigation.
推荐阅读
- azure - 如何使用 Terraform 创建 Azure AD 组成员资格?
- python - python中的异步,线程问题
- python - 如何从我的 .pem 文件中获取公钥
- html - html 的必填字段不适用于 django 模板语法
- excel - Excel 数据包络分析插件
- java - 如何将示例 Python 类编写为 Java 简单 bean?
- apache-kafka - 卡夫卡消费者组
- web-services - 访问基于肥皂的网络服务的问题
- plotly - 如何修复 Plotly Graph 对象中的 x 轴值?
- api - 从 Plaid 的 Liabilities API 将数据导入 Google 表格的快速方法?