react-navigation - react-navigation tabBarComponent 无法读取未定义的属性“键”
问题描述
react-navigation 的文档对我来说非常不清楚如何自定义tabBarComponent
不仅仅是改变颜色。我能够为选项卡创建我的自定义组件并像这样指向它;
import { TabNavigator } from 'myComponentsSomewhere'
...
const Navigator = createBottomTabNavigator(
{
Route1: Route1Component,
Route2: Route2Component,
...
},
{
tabBarComponent: props => <TabNavigator {...props} />,
tabBarOptions: {
activeTintColor: colors.first,
showLabel: false,
},
},
)
另一方面TabNavigator
,我得到了我所期望的大量道具;
activeTintColor: "#d85089",
getAccessibilityLabel,
getButtonComponent,
...
navigation,
...
在navigation
道具中,我可以到达state
然后路线等......但我无法启动任何用于获取按钮或呈现图标的功能。( renderIcon
, getButtonComponent
)
这些函数的文档很弱,但查看代码,似乎它们都需要一个包含 a 等的“路由”key
对象routeName
。
可以在navigation.state.routes
数组中找到该形状 - 但传递其中一个对象只会引发错误;
Cannot read property 'key' of undefined
这是错误代码的示例;
import React from 'react'
import { View, Text } from 'react-native'
const TabNavigator = props => {
return props.navigation.state.routes.map(route =>
props.getButtonComponent(route),
)
}
export default TabNavigator
最终,我希望能够编写自己的代码来包含选项卡,而不是仅限于将代码传递给 react-navigation 标记。我不明白为什么当直接从导航道具中传递完整的路由对象时,道具中收到的任何渲染函数都不起作用
解决方案
事实证明我没有正确阅读错误,这实际上告诉了我我需要知道的一切......
Cannot read property 'key' of undefined
getButtonComponent
(以及navigation
prop 中的所有其他获取函数)需要一个包含路由的对象,而不仅仅是路由。
getButtonComponent({ route })
而不是getButtonComponent(route)
简单胜...
推荐阅读
- javascript - 修剪和清理 Google 脚本
- python - 运行 github python 代码。它无法从自己的子文件夹之一加载代码 (PyLimitOrderBook)
- php - chr() 返回错误的字符
- c++ - 无法转换 'std::function
'到'无效*(*)(无效*)' - python - 在 Python 3.6 中创建具有 int 父类的 Dimension 自定义类时,类继承不起作用
- php - 使用 S3 在 Heroku 上上传 Laravel 5.4 图像干预
- react-native - 反应获取 POST 不起作用
- java - 尝试确定 Java 和 Tomcat 之间的相对路径位置
- swift - 使用未解析的标识符`displayMessage`
- java - Java Minesweeper - 非方板计数地雷在错误的地方