Ben_Sven_Ten
Ben_Sven_Ten

Reputation: 579

Get pathname of from where user clicked in Next.JS

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

Upvotes: 0

Views: 576

Answers (2)

GeekyCode
GeekyCode

Reputation: 265

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 (...);
}

Upvotes: 1

Ali Navidi
Ali Navidi

Reputation: 1035

with useRouter hook:

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

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

Upvotes: 1

Related Questions