userouter is null for app folder in next js 13
See original GitHub issueVerify 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:
- Created a year ago
- Comments:8 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The new
useRouter
hook should be imported fromnext/navigation
. It looks likeRedirect.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-hooksYou need to import from
next/navigation
in the new version.