From 550695a1564095a2a678627d888bfbb971e00639 Mon Sep 17 00:00:00 2001 From: Arie Trouw Date: Mon, 14 Oct 2024 12:03:50 -0700 Subject: [PATCH] Navigating in useEffect --- packages/hooks/src/useUniversalRedirect.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/hooks/src/useUniversalRedirect.ts b/packages/hooks/src/useUniversalRedirect.ts index 4251522d..680456aa 100644 --- a/packages/hooks/src/useUniversalRedirect.ts +++ b/packages/hooks/src/useUniversalRedirect.ts @@ -1,3 +1,4 @@ +import { useEffect } from 'react' import { useLocation, useNavigate } from 'react-router-dom' const DEFAULT_NAME = 'universalRedirect|to' as const @@ -12,9 +13,11 @@ export const useSetUniversalRedirect = (name: string = DEFAULT_NAME) => { export const useCheckUniversalRedirect = (name: string = DEFAULT_NAME) => { const navigate = useNavigate() - const pendingTo = localStorage.getItem(name) - if (pendingTo !== null) { - localStorage.removeItem(name) - navigate(pendingTo) - } + useEffect(() => { + const pendingTo = localStorage.getItem(name) + if (pendingTo !== null) { + localStorage.removeItem(name) + navigate(pendingTo) + } + }, [navigate]) }