From 85a659e36cd2a73613a4b9784c2f52a5aa3cb308 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 19 Jan 2025 11:51:14 +0800 Subject: [PATCH] feat: add useRedirectWithoutHistory hook --- hooks/useRedirectWithoutHistory.ts | 11 +++++++++++ hooks/useSafePush.ts | 21 --------------------- hooks/useSafeResponseSolve.ts | 15 ++++++++------- 3 files changed, 19 insertions(+), 28 deletions(-) create mode 100644 hooks/useRedirectWithoutHistory.ts delete mode 100644 hooks/useSafePush.ts diff --git a/hooks/useRedirectWithoutHistory.ts b/hooks/useRedirectWithoutHistory.ts new file mode 100644 index 0000000..1c13e61 --- /dev/null +++ b/hooks/useRedirectWithoutHistory.ts @@ -0,0 +1,11 @@ +import { StackActions } from '@react-navigation/native'; +import { router, useNavigationContainerRef, type Href } from 'expo-router'; + +export function useRedirectWithoutHistory() { + const rootNavigation = useNavigationContainerRef(); + + return (href: Href) => { + rootNavigation.dispatch(StackActions.popToTop()); + router.replace(href); + }; +} diff --git a/hooks/useSafePush.ts b/hooks/useSafePush.ts deleted file mode 100644 index e8c306d..0000000 --- a/hooks/useSafePush.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Href, usePathname, useRouter } from 'expo-router'; - -// useSafePush 适用于安全跳转,避免在同一个页面二次进行跳转 -export function useSafePush() { - const router = useRouter(); // 获取路由对象 - const pathname = usePathname(); // 获取当前路径 - - /** - * 跳转函数 - * @param targetPath 目标路由路径 - */ - const safePush = (targetPath: string) => { - if (pathname !== targetPath) { - router.push(targetPath as Href); - } else { - console.log(`已经在目标路由: ${targetPath},无需跳转`); - } - }; - - return safePush; // 返回封装的跳转函数 -} diff --git a/hooks/useSafeResponseSolve.ts b/hooks/useSafeResponseSolve.ts index d9f1c6b..475e0bb 100644 --- a/hooks/useSafeResponseSolve.ts +++ b/hooks/useSafeResponseSolve.ts @@ -1,7 +1,8 @@ -import { RejectEnum } from '@/api/enum'; import { useCallback } from 'react'; import { Alert } from 'react-native'; -import { useSafePush } from './useSafePush'; + +import { RejectEnum } from '@/api/enum'; +import { useRedirectWithoutHistory } from '@/hooks/useRedirectWithoutHistory'; interface RejectError { type: RejectEnum; // Type 被精简为只有 6 种,具体查看 api/enum.ts @@ -11,12 +12,12 @@ interface RejectError { // useSafeResponseSolve 适用于安全地处理 Axios 响应错误 // Axios 的错误对象中包含了 type 字段,这段逻辑会根据 type 的不同进行不同的处理 export const useSafeResponseSolve = () => { + const redirect = useRedirectWithoutHistory(); + /** * 处理错误的通用函数 * @param error Axios reject 抛出的错误对象 */ - const safePush = useSafePush(); - const handleError = useCallback( (error: RejectError) => { if (!error || !error.type) { @@ -34,7 +35,7 @@ export const useSafeResponseSolve = () => { [ { text: '确认', - onPress: () => safePush('/login'), // 点击确认后跳转 + onPress: () => redirect('/login'), // 点击确认后跳转 }, ], { cancelable: false }, @@ -49,7 +50,7 @@ export const useSafeResponseSolve = () => { [ { text: '确认', - onPress: () => safePush('/login'), // 点击确认后跳转 + onPress: () => redirect('/login'), // 点击确认后跳转 }, ], { cancelable: false }, @@ -82,7 +83,7 @@ export const useSafeResponseSolve = () => { break; } }, - [safePush], + [redirect], ); return { handleError };