Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJS TypeError: Cannot read property 'pathname' of undefined

I am migrating an entire app from react to next. I solved the majority of the problems but I’m having some trouble with routing.

I replaced useHistory with useRouter as we are not using react-router package anymore and I’m getting this final error on the page

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have no idea how to solve it using next routing and spent the last day going crazy with it. The location should return the current URL of the page

>Solution :

Use the useRouter hook to access the router object. router has a pathname property.

useRouter

router

pathname: String – Current route. That is the path of the page in
/pages, the configured basePath or locale is not included.

import { useRouter } from 'next/router';

...

const router = useRouter();
// router.pathname
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading