React原生:尝试将堆栈导航传递到底部选项卡导航

axr492tv  于 2023-01-21  发布在  React
关注(0)|答案(2)|浏览(137)

我在尝试将我的堆栈导航器传递到我的底部标签导航器时遇到了麻烦。我一直得到下面的错误。我哪里出错了?
我创建了Stack导航组件,然后将其导入到底部选项卡导航中,然后将组件传递到那里。

底部标签导航器

import * as React from 'react';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { createMaterialBottomTabNavigator } from '@react-navigation/material-bottom-tabs';
import { LeftNavigator } from '../stack.navigation.js/stack';

const Tab = createMaterialBottomTabNavigator();

export const BottomNavigator = () => {
 return (
     <>
 <Tab.Navigator initialRouteName="Left" activeColor="white" >
          
        <Tab.Screen name="Left Screen" component={LeftNavigator} />

       //here im trying to pass in my stack navigator component to my bottom tab navigator 
          
 </Tab.Navigator>   
     </>
 )
}

堆栈导航器

import * as React from 'react';
import { CnnScreen } from '../news.screens.js/cnn';
import  { Reuters } from '../news.screens.js/reuters';
import   { TheVerge } from '../news.screens.js/the.verge'
import { StyleSheet, Text, SafeAreaView, ScrollView, View, Image, TouchableOpacity} from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { LeftScreen } from '../left/left.screen';

import { createNativeStackNavigator } from '@react-navigation/native-stack';

const LeftStack = createNativeStackNavigator();

export const LeftNavigator = () => {
    
  <LeftStack.Navigator initialRouteName='Left'>
        <LeftStack.Screen name="Left" component={LeftScreen}/>
        <LeftStack.Screen name="CNN" component={CnnScreen}/>
        <LeftStack.Screen name="Reuters" component={Reuters}/>
        <LeftStack.Screen name="TheVerge" component={TheVerge}/>
  </LeftStack.Navigator>    

}
bqujaahr

bqujaahr1#

您代码中有两个问题。
需要修复的第一个代码:
[底部选项卡导航器]

...
    const Tab = createMaterialBottomTabNavigator();
    
    export const BottomNavigator = () => {
     return (
         <>                                 
                                            |
                                            |
                                            |
                                            *
  ---->   <Tab.Navigator initialRouteName="Left" activeColor="white" >
 replace it to: "Left Screen" as your tab screen name
              
     <Tab.Screen name="Left Screen" component={LeftNavigator} />
    
           //here im trying to pass in my stack navigator component to my bottom tab navigator 
              
     </Tab.Navigator>   
         </>
     )
    }

第二:
堆栈导航器

....
    const LeftStack = createNativeStackNavigator();
    
    export const LeftNavigator = () => {

     //---> Add return     
return <LeftStack.Navigator initialRouteName='Left'>
            <LeftStack.Screen name="Left" component={LeftScreen}/>
            <LeftStack.Screen name="CNN" component={CnnScreen}/>
            <LeftStack.Screen name="Reuters" component={Reuters}/>
            <LeftStack.Screen name="TheVerge" component={TheVerge}/>
      </LeftStack.Navigator>    
}
abithluo

abithluo2#

Answer for above comment : I added the return, and fixed the first issues as well, but now when i try to navigate to the Left it crashes? 
please pass it like this i.e pass children and remove this line component={OrderNavigatorComponent}.
<tab.Screen
    name="home"
       children={()=><ComponentName propName={propValue}/>}
    />
or 
 children={() => (
            <OrderContextProvider>
              <OrderNavigator />
            </OrderContextProvider>
          )}

=〉此处orderNavigator是您的堆栈导航器,即const OrderNavigator =()=〉{ return(<Stack.Navigator initialRouteName="order">〈堆栈屏幕名称={'order'}组件={试用版}选项={{headerShown:错误}}/〉</Stack.Navigator>); };

相关问题