TTC*_*TCG 9 react-native react-navigation react-navigation-bottom-tab
我想在按下 BottomTabNavigator 上的特定选项卡时导航到屏幕。
通常,当按下该选项卡时,它会自动导航到配置的屏幕。但我不想有这种行为。我想隐藏该屏幕上的底部选项卡,并在顶部栏中提供返回功能。navigation.navigate('routeName')我通常在 ReactNavigationStack 屏幕中使用。但我不知道如何/在哪里在 BottomTabNavigator 配置中编写此代码。
例如,我在底部栏中有以下 5 个选项卡。我想在按下“添加”按钮时导航到“AddNewScreen” 。我不知道将 onPress 事件放在哪里。我试着把它放在和下。但仍然没有运气。optionsBottomTab.Screen
我试图拦截onPress事件来使用navigation.navigate。但它甚至没有被点击,它总是用标签栏打开 AddNewScreen。
<BottomTab.Navigator initialRouteName={INITIAL_ROUTE_NAME}>
<BottomTab.Screen
name="Home"
component={HomeScreen}
initialParams="Home Params"
options={{
title: 'Home',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-home" iconType="ion" />,
}}
/>
<BottomTab.Screen
name="AddNew"
component={AddNewScreen}
options={{
title: 'Add',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-add-circle" iconType="ion"
onPress={(e) => {
e.preventDefault();
console.log(e)
}} />,
}}
/>
</BottomTab.Navigator>
Run Code Online (Sandbox Code Playgroud)
添加新屏幕始终通过底部选项卡栏打开。
问题:
Gur*_*ran 19
导航库 v6 支持可使用的Listener功能
<Tab.Screen
name="Chat"
component={Chat}
listeners={{
tabPress: e => {
// Prevent default action
e.preventDefault();
//Any custom code here
alert(123);
},
}}
/>;
Run Code Online (Sandbox Code Playgroud)
您可以使用选项卡按钮在底部工具栏中拥有自定义功能。代码如下所示
<Tab.Screen
name="Settings2"
component={SettingsScreen}
options={{
tabBarButton: props => (
<TouchableOpacity {...props} onPress={() => alert(123)} />
),
}}
/>
Run Code Online (Sandbox Code Playgroud)
这将呈现一个正常的底部选项卡栏按钮,但 onclick 会显示警报,您可以用导航或您需要的任何其他代码替换该代码。此外,“SettingsScreen”组件可以是返回 null 的虚拟组件。
希望这可以帮助。
小智 15
您可以拥有自定义功能
<Tab.Screen
name="Add"
component={View}
listeners={({ navigation }) => ({
tabPress: (e) => {
// Prevent default action
e.preventDefault();
// Do something with the `navigation` object
navigation.navigate("PhotoNavigation"); // Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!
},
})}
/>
<Tab.Screen name="Notifications" component={Notifications} />Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20350 次 |
| 最近记录: |