Skip to content

Commit

Permalink
Navigating in useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Oct 14, 2024
1 parent 949a6ac commit 550695a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/hooks/src/useUniversalRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'

const DEFAULT_NAME = 'universalRedirect|to' as const
Expand All @@ -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])
}

0 comments on commit 550695a

Please sign in to comment.