Last active
August 4, 2023 02:14
-
-
Save kamikat/5a81c7c084ea511ea9de742929f99ed1 to your computer and use it in GitHub Desktop.
Drop-in replacement of `useLocation()`/`useNavigate()` hooks in browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useLayoutEffect, useMemo, useReducer } from "react"; | |
function createHooks() { | |
const listeners: (() => void)[] = []; | |
window.addEventListener("popstate", () => { | |
for (const listener of listeners) { | |
listener(); | |
} | |
}); | |
function useLocation(): Location { | |
const [_updateId, notifyUpdate] = useReducer((a) => a + 1, 0); | |
useLayoutEffect(() => { | |
const listener = () => notifyUpdate(); | |
listeners.push(listener); | |
return () => { | |
const index = listeners.indexOf(listener); | |
if (index > -1) { | |
listeners.splice(index, 1); | |
} | |
}; | |
}, [notifyUpdate]); | |
const location: Location = useMemo(() => { | |
return { _updateId, ...window.location }; | |
}, [_updateId]); | |
return location; | |
} | |
function useNavigate(options?: { replace?: boolean }) { | |
const isReplace = options?.replace; | |
return useCallback( | |
(path: string) => { | |
if (isReplace) { | |
window.history.replaceState(null, "", path); | |
} else { | |
window.history.pushState(null, "", path); | |
} | |
window.dispatchEvent(new Event("popstate")); | |
}, | |
[isReplace] | |
); | |
} | |
return { useLocation, useNavigate }; | |
} | |
const { useLocation, useNavigate } = createHooks(); | |
export { useLocation, useNavigate }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment