如何获取子导航的路线名称

时间:2020-09-06 15:22:06

标签: react-native

BottomTabNavigator.tsx

import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

   <NavigationContainer>
     <Tab.Navigator>
        <Tab.Screen name="Lecture" component={LectureNavigator} />
    </Tab.Navigator>
  </NavigationContainer>

LectureNavigator.tsx

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

const Stack = createStackNavigator();

const LectureNavigator = ({ navigation, route }:any) => {

    React.useEffect(() => {

        const backAction = () => {
            
            const state = navigation.dangerouslyGetState();
            let actualRoute = state.routes[state.index];

            console.log(actualRoute);

            //result of actualRoute
            // Object {
            //           "key": "Lecture--LNI9xd5xa5iB4_h7J5c2",
            //           "name": "Lecture",
            //           "params": Object {
            //                               "initial": false,
            //                               "screen": "CourseList",
            //                   },
            //           }
            
            return true;
        };
    
        const backHandler = BackHandler.addEventListener(
          "hardwareBackPress",
          backAction
        );
    
        return () => backHandler.remove();
    }, [navigation]);


      
    return (
        <NavigationContainer independent={true}>
            <Stack.Navigator>
                <Stack.Screen name="CourseList" component={CourseList} />
                <Stack.Screen name="LectureList" component={LectureList} />
                <Stack.Screen name="LectureView" component={LectureView} />
            </Stack.Navigator>
        </NavigationContainer>
    )
}

export default LectureNavigator;

父导航是底部导航,而内部导航是LectureNavigator。

我想在LectureNavigator.tsx上获取内部导航的路线名称。

但是我无法从父导航访问LectureNavigator。

有什么好的解决方法吗?

我想获取路线名称,例如CourseList,LectureList或LectureView。不是父母的路线名称。

感谢您阅读

0 个答案:

没有答案