question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

userouter is null for app folder in next js 13

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information


    Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP Fri Apr 2 22:23:49 UTC 2021
    Binaries:
      Node: 16.15.0
      npm: 8.19.2
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.0.0
      eslint-config-next: 13.0.0
      react: 18.2.0
      react-dom: 18.2.0

warn  - Latest canary version not detected, detected: "13.0.0", newest: "12.3.2-canary.43".

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

I am using a redirect component in the new app directory where useRouter is coming off as null

app/pages.tsx

'use client'
import { Redirect } from 'src/util/Redirect'
export default Redirect

src/component/Redirect.tsx

'use client'

import { useEffect } from 'react'
import { useRouter } from 'next/router'
import languageDetector from './languageDetector'

export const useRedirect = (to?: string) => {
  const router = useRouter()
//router here is null
  to = to || router.asPath

  // language detection
  useEffect(() => {
    const detectedLng = languageDetector.detect()
    if (to?.startsWith('/' + detectedLng) && router.route === '/404') {
      // prevent endless loop
      router.replace('/' + detectedLng + router.route)
      return
    }

    languageDetector.cache?.(detectedLng ?? 'en')
    router.replace('/' + detectedLng + to)
  })

  return <></>
}

export const Redirect = () => {
  useRedirect()
  return <></>
}

Expected Behavior

useRouter should return the router object

Link to reproduction

https://stackblitz.com/edit/github-fdiksh-6amcyc?file=app%2Fpage.ts,src%2FRedirect.ts

To Reproduce

Mentioned above

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
HW13commented, Oct 25, 2022

The new useRouter hook should be imported from next/navigation. It looks like Redirect.tsx is importing from the old hook’s location (next/router). The beta docs have a guide on migrating to the new hook -> https://beta.nextjs.org/docs/upgrade-guide#step-4-migrating-routing-hooks

4reactions
dephraiimcommented, Oct 26, 2022

You need to import from next/navigation in the new version.

import { useRouter } from 'next/navigation';
Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter is null; router.isready does not seem to work with ...
Im trying out next js 13 and I got ...
Read more >
next/router | Next.js
useRouter is a React Hook, meaning it cannot be used with classes. You can either use withRouter or wrap your class in a...
Read more >
next-router-mock - npm
Mock implementation of the Next.js Router. ... import singletonRouter, { useRouter } from 'next/router'; import NextLink from 'next/link'; ...
Read more >
Route prefetching in Next.js - web.dev
To implement prefetching in your routing code, use the prefetch method from useRouter . Take a look at components/MyLink.js in this example app: ......
Read more >
Next.js 11 - User Registration and Login Tutorial with Example ...
js tutorial app data. helpers. Anything that doesn't fit into the other folders and doesn't justify having its own folder. Front-end React ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found