Home > OS >  Get pathname of from where user clicked in Next.JS
Get pathname of from where user clicked in Next.JS

Time:04-25

I have a button in a <Nav/> component that is shared between two components. when the button is clicked it opens up a <FileUpload/> component.

<FileUpload/> can be accessed from:

<Profile /> => routes to '/profile'

<Home /> => routes to '/home'

inside <FileUpload/> is there any way to check which route the user has just come from?

so inside the <FileUpload/> component I can see:

`user has just come from ${route} route` => either /profile or /home

CodePudding user response:

with useRouter hook:

const router = useRouter()
console.log(router)

it gives you a lot of information check if your answer is in it;

CodePudding user response:

inside your FileUpload component import useRouter hook at the top from next/router then use it inside your component to get the pathname like this:

import { useRouter } from 'next/router'

const FileUpload = ()=> {
   const router = useRouter()
   const {pathname} = router;
  
   console.log("user come from: "   pathname)

  return (...);
}
  • Related