react-native - navigation.getParent() 总是返回 undefined
问题描述
我在 tabNavigator 中有一个组件,并且想要导航到父堆栈中的另一个组件。但是,每当我尝试使用时,navigation.getParent.navigate(routeName)
我都会收到错误消息TypeError: undefined is not an object (evaluating 'navigation.getParent().navigate')
。
下面是我的导航器的样子:
<NavigationContainer independent={true}>
<Tab.Navigator
initialRouteName="Feed"
initialLayout={{ width: Dimensions.get('window').width }}
screenOptions={{
tabBarActiveTintColor: Colors.white,
tabBarInactiveTintColor: Colors.gray_2,
activeBackgroundColor: Colors.blue_primary,
tabBarScrollEnabled: true,
headerShown: false,
tabBarIndicatorStyle: {
width: 0,
height: 0,
elevation: 0,
},
tabBarItemStyle: { alignItems: 'center', },
tabBarLabelStyle: { margin: 0, fontSize: fontSize.xsmall, fontFamily: fontFamily.EuclidCircularARegular, color: Colors.gray_2 },
tabBarStyle: {
backgroundColor: Colors.white,
padding: 0,
elevation: 0, // for Android
shadowOffset: {
width: 0,
height: 0 // for iOS
},
width: '100%'
}
}}
>
<Tab.Screen
name={'For you'}
component={DiscoverContent}
initialParams={{ id: 177 }}
options={{
tabBarLabel: ({ color, focused }) => (
<Text style={[styles.label, { color: color, backgroundColor: focused ? Colors.blue_primary : Colors.white }]}>For you</Text>
),
}}
/>
{
usersInterests.map((item) => (
<Tab.Screen
key={item.id}
name={item.name}
component={DiscoverContent}
initialParams={{ id: item.id }}
options={{
tabBarLabel: ({ color, focused }) => (
<Text style={[styles.label, { color: color, backgroundColor: focused ? Colors.blue_primary : Colors.white }]}>{item.name}</Text>
),
}}
/>
))
}
</Tab.Navigator>
</NavigationContainer>
堆栈导航器:
<Stack.Navigator
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="interests" component={Interests} />
<Stack.Screen name="importSubscriptions" component={ImportSubscriptions} />
<Stack.Screen name="subscriptions" component={Subscriptions} />
<Stack.Screen name="bottomTab" component={MainBottomTab} />
<Stack.Screen name="podcastItem" component={PodcastItemCard}/>
<Stack.Screen name="player" component={Player} />
<Stack.Screen name="tabNavigator" component={UsersInterestTab} />
</Stack.Navigator>
解决方案
推荐阅读
- locking - Teradata 7423:HY000] 对象已锁定,现在等待。交易中止
- python - 在 python 中验证 sys.argv[] 参数
- c++ - 使用具有员工详细信息(姓名,ID,职位,薪水)的类的朋友函数查找最高薪水并打印最高薪水详细信息
- batch-file - 当 .sln 文件具有“任何 CPU”时,如何从 .bat 文件中恢复 nuget 包?
- vue.js - Vue.js - 过滤多个字段
- mongodb - 如何确保我的聚合过滤掉 mongo 中过期的子文档?
- citrix - cloudstack 是否支持 Citrix Hypervisor 7 和 8?
- html - 悬停时旋转项目并在鼠标离开时保留该位置
- python - 如何构建一个查询来检索与课程严格匹配的课程模块?
- sql - 如何使具有多个 if 的过程更短且高效?