@react-navigation/native 和@react-navigation/stack 读取整个堆栈

@react-navigation/native and @react-navigation/stack read whole stack

好的,所以我有我的 App.js

<NavigationContainer>
  <Stack.Navigator initialRouteName="Home">
    <Stack.Screen name="Home" component={HomeScreen} />
    <Stack.Screen name="Main" component={MainScreen} />
    <Stack.Screen name="Login" component={LoginScreen} />
    <Stack.Screen name="Pokus" component={PokusScreen} />
  </Stack.Navigator>
</NavigationContainer>;

然后在我的 Home 组件中,我重定向到 Main 组件。

我是这样传递导航的:

export default function Home({ navigation })

我的问题是:我现在可以读取堆栈了吗?

我可以这样说吗:navigation.ReadStack()?

我想看什么?:我想看{ { name: "Home", index: 0 }, { name: "Main", index: 1 } }

这可能吗?

感谢您的帮助!

编辑:我在主要组件中的代码

import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { last_activity_token, UpdateActivity } from '../Models/activityModel';
import {useNavigationState} from '@react-navigation/native';

export default function Main({ navigation }) {
  //constanty -- constants
  const [activity, setActivity] = useState(new last_activity_token());

  const Pokus = () => {
    const state = useNavigationState(state => state);
    alert(JSON.stringify(state.routes))
  }

  //Load event
  React.useEffect(() => {
    activity.page.name = "Main";
    UpdateActivity(activity);
  });

  //vzhled stránky -- design of page 
  return (
      <View style={styles.layout}>
        <Text>Main</Text>
        <View style={styles.MainContent}>
          <Text>Tady se to vše bude odehrávat</Text>
          <Button onPress={Pokus}/>
        </View>
      </View>
    );
}

//css
const styles = StyleSheet.create({
    layout: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
    },
    MainContent: {
      marginTop: 20
    }
});

您可以使用如下所示的 useNavigationState 挂钩获取当前状态

https://reactnavigation.org/docs/use-navigation-state/

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

const state = useNavigationState((state) => state);

alert(JSON.stringify(state.routes));

这将在路线中给出屏幕输出