import React, { useRef } from 'react'; import { NavigationContainer, NavigationState, PartialState } from "@react-navigation/native"; import { createNativeStackNavigator } from "@react-navigation/native-stack"; import { RootStackParamList } from './types'; import * as Screens from './screens'; import Toast from "react-native-toast-message"; const Stack = createNativeStackNavigator(); // 获取当前路由信息的工具函数 // [DEBUG-ROUTER-LOGGER] 路由跟踪函数 - 生产环境可删除 const getActiveRouteName = (state: NavigationState | PartialState | undefined): string => { if (!state || !state.routes) return ''; const route = state.routes[state.index || 0]; // 检查是否存在嵌套导航 if (route.state && route.state.routes) { return getActiveRouteName(route.state); } return route.name; }; // 获取路由的完整路径 // [DEBUG-ROUTER-LOGGER] 路由跟踪函数 - 生产环境可删除 const getRoutePath = (state: NavigationState | PartialState | undefined): string[] => { if (!state || !state.routes) return []; const route = state.routes[state.index || 0]; const currentPath = [route.name]; // 检查是否存在嵌套导航 if (route.state && route.state.routes) { return [...currentPath, ...getRoutePath(route.state)]; } return currentPath; }; export const AppNavigator = () => { // [DEBUG-ROUTER-LOGGER] 路由跟踪引用 - 生产环境可删除 const navigationRef = useRef(null); const routeNameRef = useRef(); return ( { // [DEBUG-ROUTER-LOGGER] 初始路由日志 - 生产环境可删除 - 开始 routeNameRef.current = getActiveRouteName(navigationRef.current?.getRootState()); console.log('[DEBUG-ROUTER] 初始路由:', routeNameRef.current); // 打印组件信息 const componentInfo = Screens[routeNameRef.current as keyof typeof Screens]; console.log('[DEBUG-ROUTER] 组件信息:', componentInfo ? componentInfo.name || '未命名组件' : '未找到组件'); // [DEBUG-ROUTER-LOGGER] 初始路由日志 - 生产环境可删除 - 结束 }} onStateChange={(state) => { // [DEBUG-ROUTER-LOGGER] 路由变化日志 - 生产环境可删除 - 开始 const previousRouteName = routeNameRef.current; const currentRouteName = getActiveRouteName(state); if (previousRouteName !== currentRouteName) { // 记录路由变化 console.log(`[DEBUG-ROUTER] 路由变化: ${previousRouteName} -> ${currentRouteName}`); // 打印完整路径 const fullPath = getRoutePath(state); console.log('[DEBUG-ROUTER] 路由完整路径:', fullPath.join(' -> ')); // 打印组件信息 const componentInfo = Screens[currentRouteName as keyof typeof Screens]; console.log('[DEBUG-ROUTER] 组件信息:', componentInfo ? componentInfo.name || '未命名组件' : '未找到组件'); // 打印路由参数信息 const currentRoute = state?.routes?.[state.index || 0]; if (currentRoute && currentRoute.params) { console.log('[DEBUG-ROUTER] 路由参数:', JSON.stringify(currentRoute.params, null, 2)); } // 更新当前路由名称引用 routeNameRef.current = currentRouteName; } // [DEBUG-ROUTER-LOGGER] 路由变化日志 - 生产环境可删除 - 结束 }} > ); };