Last active
September 22, 2022 21:10
-
-
Save brandonchadlange/39af50b024c6f09999aeb351ec080538 to your computer and use it in GitHub Desktop.
Next.js custom router hook
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 { useEffect, useState } from "react"; | |
const PATH = "discovery"; | |
const useDiscoveryRouter = () => { | |
const [organisation, setOrganisation] = useState<string>(""); | |
const [workspace, setWorkspace] = useState<string>(""); | |
const router = useRouter(); | |
useEffect(() => { | |
setOrganisation(router.query.organisation as string); | |
setWorkspace(router.query.workspace as string); | |
}, [router.query]); | |
const pathPrefix = `/${organisation}/${workspace}/${PATH}`; | |
return { | |
landingPage() { | |
return pathPrefix; | |
}, | |
createDiscovery() { | |
return pathPrefix + "/create"; | |
}, | |
editDiscovery(discovery: string) { | |
return pathPrefix + `/${discovery}`; | |
}, | |
}; | |
}; | |
export default useDiscoveryRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment