From 2321c2dc06c91979a48634c06188fbbebc7bd56f Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Wed, 18 Mar 2026 14:52:58 +0800 Subject: [PATCH] refactor(navigation): replace direct navigation with navigation service Replace direct `navigation.navigate()` and `navigation.getParent()?.navigate()` calls with centralized `navigationService.navigate()` across multiple screens. Convert screen component functions from JSX elements to array-returning functions in RootNavigator to fix React key warnings and improve render consistency. - HomeScreen: use navigationService for post and user navigation - PrivateChatInfoScreen: use navigationService for profile navigation - ChatScreen: use navigationService for avatar press and user profile navigation - RootNavigator: convert PublicScreens and AuthenticatedScreens to getPublicScreens() and getAuthenticatedScreens() functions returning arrays with proper keys --- src/navigation/RootNavigator.tsx | 332 +++++++++--------- src/screens/home/HomeScreen.tsx | 5 +- src/screens/message/PrivateChatInfoScreen.tsx | 6 +- .../components/ChatScreen/useChatScreen.ts | 8 +- 4 files changed, 181 insertions(+), 170 deletions(-) diff --git a/src/navigation/RootNavigator.tsx b/src/navigation/RootNavigator.tsx index 871dbcb..c996800 100644 --- a/src/navigation/RootNavigator.tsx +++ b/src/navigation/RootNavigator.tsx @@ -41,166 +41,176 @@ interface RootNavigatorProps { isInitializing: boolean; } -// 未认证时可访问的屏幕 -const PublicScreens = () => ( - <> - - - -); +// 未认证时可访问的屏幕 - 返回数组而不是 JSX +const getPublicScreens = () => [ + , + , +]; -// 认证后可访问的屏幕 -const AuthenticatedScreens = () => ( - <> - - - - - - - - - - - - - -); +// 认证后可访问的屏幕 - 返回数组而不是 JSX +const getAuthenticatedScreens = () => [ + , + , + , + , + , + , + , + , + , + , + , + , +]; export function RootNavigator({ isAuthenticated, isInitializing }: RootNavigatorProps) { const { isMobile } = useResponsive(); @@ -238,7 +248,7 @@ export function RootNavigator({ isAuthenticated, isInitializing }: RootNavigator ) } - + {getAuthenticatedScreens()} ) : ( <> @@ -247,7 +257,7 @@ export function RootNavigator({ isAuthenticated, isInitializing }: RootNavigator component={AuthNavigator} options={{ headerShown: false }} /> - + {getPublicScreens()} )} diff --git a/src/screens/home/HomeScreen.tsx b/src/screens/home/HomeScreen.tsx index cd5b1b5..c55e5ad 100644 --- a/src/screens/home/HomeScreen.tsx +++ b/src/screens/home/HomeScreen.tsx @@ -35,6 +35,7 @@ import { HomeStackParamList, RootStackParamList } from '../../navigation/types'; import { useResponsive, useResponsiveSpacing } from '../../hooks/useResponsive'; import { SearchScreen } from './SearchScreen'; import { CreatePostScreen } from '../create/CreatePostScreen'; +import { navigationService } from '../../infrastructure/navigation/navigationService'; type NavigationProp = NativeStackNavigationProp & NativeStackNavigationProp; @@ -343,12 +344,12 @@ export const HomeScreen: React.FC = () => { // 跳转到帖子详情 const handlePostPress = (postId: string, scrollToComments: boolean = false) => { - navigation.getParent()?.navigate('PostDetail', { postId, scrollToComments }); + navigationService.navigate('PostDetail', { postId, scrollToComments }); }; // 跳转到用户主页 const handleUserPress = (userId: string) => { - navigation.getParent()?.navigate('UserProfile', { userId }); + navigationService.navigate('UserProfile', { userId }); }; // 点赞帖子 diff --git a/src/screens/message/PrivateChatInfoScreen.tsx b/src/screens/message/PrivateChatInfoScreen.tsx index e3aebbe..0a8c5e3 100644 --- a/src/screens/message/PrivateChatInfoScreen.tsx +++ b/src/screens/message/PrivateChatInfoScreen.tsx @@ -31,6 +31,7 @@ import { userManager } from '../../stores/userManager'; import { Avatar, Text, Button, Loading, Divider } from '../../components/common'; import { User } from '../../types'; import { RootStackParamList, MessageStackParamList } from '../../navigation/types'; +import { navigationService } from '../../infrastructure/navigation/navigationService'; type NavigationProp = NativeStackNavigationProp; type PrivateChatInfoRouteProp = RouteProp; @@ -149,9 +150,8 @@ const PrivateChatInfoScreen: React.FC = () => { // 查看用户资料 const handleViewProfile = () => { - // 使用根导航跳转到用户资料页面 - const rootNavigation = navigation.getParent(); - rootNavigation?.navigate('UserProfile' as any, { userId }); + // 使用导航服务跳转到用户资料页面 + navigationService.navigate('UserProfile', { userId }); }; // 举报/投诉 diff --git a/src/screens/message/components/ChatScreen/useChatScreen.ts b/src/screens/message/components/ChatScreen/useChatScreen.ts index 2200f19..33ab6e7 100644 --- a/src/screens/message/components/ChatScreen/useChatScreen.ts +++ b/src/screens/message/components/ChatScreen/useChatScreen.ts @@ -31,6 +31,7 @@ import { groupService } from '../../../../services/groupService'; import { userManager } from '../../../../stores/userManager'; import { groupManager } from '../../../../stores/groupManager'; import { RootStackParamList } from '../../../../navigation/types'; +import { navigationService } from '../../../../infrastructure/navigation/navigationService'; import { GroupMessage, PanelType, @@ -1039,9 +1040,9 @@ export const useChatScreen = () => { // 点击头像跳转到用户主页 const handleAvatarPress = useCallback((userId: string) => { if (userId && userId !== currentUserId) { - navigation.navigate('UserProfile' as any, { userId: String(userId) }); + navigationService.navigate('UserProfile', { userId: String(userId) }); } - }, [navigation, currentUserId]); + }, [currentUserId]); // 长按头像@用户 const handleAvatarLongPress = useCallback((senderId: string, nickname: string) => { @@ -1113,8 +1114,7 @@ export const useChatScreen = () => { conversationId: conversationId || undefined, }); } else if (otherUser?.id) { - const rootNavigation = navigation.getParent(); - rootNavigation?.navigate('UserProfile' as any, { userId: String(otherUser.id) }); + navigationService.navigate('UserProfile', { userId: String(otherUser.id) }); } }, [navigation, isGroupChat, routeGroupId, otherUser]);