【问题标题】:React Native Navigation. How to add component to bottom stack navigator but not display it反应本机导航。如何将组件添加到底部堆栈导航器但不显示它
【发布时间】:2021-12-13 15:24:28
【问题描述】:

我想在底部堆栈导航器中添加一个组件(比如说“Autobus”),这样它将接收“navigation”参数,并且我将能够导航到“Autobus”组件 - navigation.navigate('Autbous')。但是,我不想在“Tab.Navigator”中实际显示“Autobus”组件。

我有以下代码:

import React from 'react'
import { Ionicons } from '@expo/vector-icons'
import { StyleSheet, View, TouchableOpacity } from 'react-native'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'

import THEME from '../theme'
import PostsScreen from '../screens/PostsScreen'
import CreateScreen from '../screens/CreateScreen'
import ProfileScreen from '../screens/ProfileScreen'

const Tab = createBottomTabNavigator()

const Tabs = () => {
    return (
        <Tab.Navigator
            initialRouteName='Posts'
            screenOptions={{
                tabBarShowLabel: false,
                tabBarStyle: { ...styles.tab, ...styles.shadow }
            }}
        >
            <Tab.Screen
                name='Profile'
                component={ProfileScreen}
                options={{
                    tabBarIcon: ({ focused }) => (
                        <CustomTabIcon focused={focused} iconName='person' />
                    )
                }}
            />
            <Tab.Screen
                name='Create'
                component={CreateScreen}
                options={{
                    // tabBarShowLabel: true,
                    tabBarIcon: ({ focused }) => (
                        <CustomTabIcon focused={focused} iconName='add' />
                    ),
                    tabBarButton: props => <CustomCircleButton {...props} />
                }}
            />
            <Tab.Screen
                name='Posts'
                component={PostsScreen}
                options={{
                    tabBarIcon: ({ focused }) => (
                        <CustomTabIcon focused={focused} iconName='search' />
                    )
                }}
            />
        </Tab.Navigator>
    )
}

const CustomTabIcon = ({ focused, iconName }) => {
    return (
        <Ionicons
            size={24}
            color={THEME.INFO_COLOR}
            name={focused ? iconName : `${iconName}-outline`}
        />
    )
}

const CustomCircleButton = ({ children, onPress }) => {
    return (
        <TouchableOpacity
            onPress={onPress}
            style={{
                ...styles.center,
                ...styles.shadow
            }}
        >
            <View style={styles.circle}>{children}</View>
        </TouchableOpacity>
    )
}

const styles = StyleSheet.create({
    tab: {
        position: 'absolute',
        bottom: 15,
        left: 20,
        right: 20,
        elevation: 0,
        backgroundColor: 'white',
        borderRadius: 15,
        height: 70
    },
    shadow: {
        shadowColor: '#7f5df0',
        shadowOffset: {
            width: 0,
            height: 10
        },
        shadowOpacity: 0.25,
        shadowRadius: 3.5,
        elevation: 5
    },
    center: {
        top: -20,
        justifyContent: 'center',
        alignItems: 'center'
    },
    circle: {
        width: 70,
        height: 70,
        borderRadius: 35,
        backgroundColor: THEME.DANGER_COLOR
    }
})

export default Tabs

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    您需要为嵌套导航创建堆栈导航器(底部标签/顶部标签/单独的屏幕)

    阅读更多https://reactnavigation.org/docs/nesting-navigators/

    import { NavigationContainer } from '@react-navigation/native';
    import { createNativeStackNavigator } from '@react-navigation/native-stack';
    
    const Tab = createBottomTabNavigator();
    
    const Stack = createNativeStackNavigator();  
    
    
    const Tabs = () => {
        return (
            <Tab.Navigator initialRouteName='Posts'>
                <Tab.Screen name='Profile' component={ProfileScreen}/>
                <Tab.Screen name='Create' component={CreateScreen}/>
                <Tab.Screen name='Posts' component={PostsScreen}/>
            </Tab.Navigator>
        )
    }
    
    const App = () => {
       return(
        <NavigationContainer>
         <Stack.Navigator initialRouteName='Tabs'>
          <Stack.Screen name="Tabs" component={Tabs} /> // bottom tabs will be base screen
          <Stack.Screen name = "Autobus" commponent={Autobus} />
        </Stack.Navigator>
      </NavigationContainer>
      )
    }
    

    【讨论】:

    • 谢谢,它解决了我的问题 但是,您在 App 组件中有一个错字 ->
    • 对不起我的错....没有检查错字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多