Created
February 26, 2024 20:27
-
-
Save isocroft/b45ec84dbee6966b0e8ec8f218daeb9e to your computer and use it in GitHub Desktop.
A custom NextJS hook that returns the previous route visited in a browser application
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 { useRouter } from "next/router"; | |
import { useRef } from "react"; | |
export const useNexJSPreviousRoute = () => { | |
const router = useRouter(); | |
const ref = useRef<string | null>(null); | |
router.events?.on("routeChangeStart", () => { | |
ref.current = router.asPath; | |
}); | |
return ref.current; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment