Cannot use default (singleton) router: "You should only use "next/router" on the client side of your app"
See original GitHub issueHello! First of all thanks for awesome package, @scottrippey!
I try to mock next-router with storybook. Is it real to mock default Router as we do it for useRouter?
For instance useRouter works fine:
useRouter example
import { useCallback } from 'react'
import { useRouter } from 'next/router'
const UseRouterPage = () => {
const router = useRouter()
const handleClick = useCallback(() => router.push({
pathname: router?.pathname,
query: {
...router?.query,
test: '1'
},
}, undefined,
{
scroll: false,
shallow: true
}), [])
return (
<main>
<button onClick={handleClick}>button</button>
</main>
)
}
export default UseRouterPage
But with default exported Router, the next error appears:
I try use default exported Router because in this case there is no component re-render.
useRouter example
import { useCallback } from 'react'
import Router from 'next/router'
const RouterPage = () => {
const handleClick = useCallback(() => Router.push({
pathname: Router?.pathname,
query: {
...Router?.query,
test: '1'
},
}, undefined,
{
scroll: false,
shallow: true
}), [])
return (
<main>
<button onClick={handleClick}>button</button>
</main>
)
}
export default RouterPage
Repo to reproduce: https://github.com/sedlukha/next-router-mock-example
Issue Analytics
- State:
- Created a year ago
- Comments:19 (9 by maintainers)
Top Results From Across the Web
No router instance found. You should only use "next ... - GitHub
You should only use "next/router" inside the client side of your app. #6713 ... import Router from 'next/router' export default (props) ...
Read more >import Router from 'next/router' is it ok? - Stack Overflow
You will receive an error with: You should only use "next/router" inside the client side of your app . This is because of...
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 js router push without reload - Caritas Castellaneta
In this short article we'll cover how to redirect to internal and external pages, if you're using Vue Router and if you're just...
Read more >next-router-mock - npm
Start using next-router-mock in your project by running `npm i ... If you want to mock next/link , you should also include this...
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
@amolagre I had the same
ModuleNotFoundError
error as you, but it seems to work by importing thenext-12
version specifically:Hi @scottrippey , Any update to make
next/link
(12.2) working with storybook. I’m getting this error while runningnpm run storybook
ModuleNotFoundError: Module not found: Error: Can't resolve 'next/dist/next-server/lib/router-context' in 'my-project\node_modules\next-router-mock\dist\MemoryRouterProvider'