react-native - React Native:无法读取未定义的属性“导航”
问题描述
我正在尝试制作一个 onPress 功能,以便在单击菜单图标时,我的抽屉导航将打开。但是,我收到此错误:未处理的 JS 异常:无法读取未定义的属性“导航”。
这是我的 HeaderComponent.js 代码
import React from 'react';
import { StyleSheet } from 'react-native';
import { Header, Left, Icon, Body, Title, Right } from 'native-base';
const HeaderComponent = (props) => {
const { menuIconStyle } = styles;
return (
<Header>
<Left>
<Icon
style={menuIconStyle}
name="menu"
onPress={() => this.props.navigation.openDrawer()}
/>
</Left>
<Body>
<Title>{props.headerText}</Title>
</Body>
<Right>
</Right>
</Header>
);
}
export default HeaderComponent;
const styles = StyleSheet.create({
menuIconStyle: {
paddingLeft: 15,
},
});
将不胜感激一个解决方案。谢谢!
解决方案
您应该将导航道具从父级传递给子级,如下所示:
<HeaderComponent navigation={this.props.navigation} />
然后你可以在你的子组件中使用导航
推荐阅读
- paypal - PayPal REST SDK 不支持定期付款
- android - 带有 UUID 过滤器列表混淆的 Android BLE 扫描
- angular - 错误类型错误:无法读取 null 的属性“替换”(无限循环)
- java - 从 url 字符串获取 HandlerMethod
- javascript - Thead 在打印时不在页眉上重复
- r - Graphviz 正在绘制带有 misig 箭头和节点圆形框架的 DAG
- google-cloud-platform - GCP VMWare 引擎 - 与 Vsphere 客户端的连接不起作用
- python - 运行此 python 代码时出现错误。错误是“ElementNotInteractableException”。任何人都可以帮助我吗?
- python - 如何在 Python 中使用 SPACE 键进行图像交换
- jmeter - Jmeter-虽然控制器没有退出循环