Home > Blockchain >  In Nextjs, how to extract what's in the location bar?
In Nextjs, how to extract what's in the location bar?

Time:10-01

I'm following TypeError: Cannot read property 'location' of undefined

**Question: is there someway to extract what's in the location bar in Nextjs because useLocation() is not working.

Thanks!

CodePudding user response:

I guess that useLocation() try to access window.location, that is not defined in SSR. You should import your component dynamically with SSR disabled : for example in your page :

import dynamic from 'next/dynamic'

const DynamicComponentWithNoSSR = dynamic(
  () => import('../components/GoogleAuthCallback'), // <- path of your component
  { ssr: false } 
) 

Please note that you can also use default next.js router :

import { useRouter } from 'next/router'
function GoogleAuthCallback() {
  const [auth, setAuth] = useState()
   const router = useRouter()
   const location = router.pathname // or router.asPath
  ....
}
  • Related