Comments (3)
I'm currently on vacation without access to my computer, so I won't be able to test this until next week. But I have some questions in the meantime.
How confident are you that in Next, the useRouter
reference doesn't change?
I intentionally added the "change the reference" behavior, based on what I thought Next was doing. I even added unit tests for this scenario.
Maybe the behavior has changed between Next versions?
One big reason I ask is because router.query
could change between renders, but if you used just router
in your dependencies, it wouldn't trigger the dependency change. Maybe Next doesn't care, but I'm pretty sure that's how it worked when I was testing it last.
I could definitely be wrong, but I want to make sure.
from next-router-mock.
I created a sandbox to test this issue: https://codesandbox.io/s/test-next-router-reference-lqbn0i
What I'm seeing: whenever I call router.push(...)
, the component re-renders, and the useRouter()
hook returns a NEW reference each time the router.query
changes. When non-route-related changes occur (eg. setState
is called), the router reference doesn't change.
This is what next-router-mock
accurately reproduces. So I think this behavior needs to stay as-is.
Please let me know if you can reproduce different behavior from Next, or perhaps if a different version of Next behaves differently. Thanks!
from next-router-mock.
Sorry for the late reply.
You are indeed correct; I think since the page component usually unmounts after invoking router.push
or router.replace
, we did not encounter the problem of infinite rendering.
It seems that having router.replace
in the deps of useEffect (#37 (comment)) is not an option because it also runs into infinite rerendering (Codesandbox). Also ESLint warns about the incorrect deps problem (I remember this is due to the notorious this
in JS).
It might be better for us to create a fake Next.js app which simply unmounts the component when router.pathname
changes. I think this matches the actual behavior of Next.js.
Thanks for looking deeply into the problem and helping us 👍
from next-router-mock.
Related Issues (20)
- `next/link` not working with `passHref` HOT 1
- Next 13.4.20-canary.20 issue HOT 8
- MemoryRouterProvider url property does not work properly HOT 3
- Incorrect path in types
- With Next14, the path to the RouterContext seems to be wrong. HOT 2
- next/link not working with async HOT 2
- Support Next 14.1 shallow routing
- configuring webpack alias in storybook, it doesn't to work in storybook test HOT 9
- `router.back` support HOT 4
- Any support planned for `next` version `13.2.x` ? HOT 3
- Add a changelog HOT 2
- In 0.9.3 test asserting `window.location.assign` fails HOT 1
- Is it possible to use dynamic routes with the MemoryRouterProvider for rendering in storybook? HOT 3
- dist folder auto-import inconsistencies HOT 2
- Using `next/link` with the locale property. HOT 1
- How to mock next router with jest `experimental-vm-modules` HOT 3
- router.push({ hash }) not triggering a re-render HOT 2
- Inconsistent useRouter behavior compared to the Next.js Router HOT 7
- Consider the skipTrailingSlashRedirect config HOT 1
- mocked next/link behavior seems inconsistenst with next/link from next.js when the `as` prop is used HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-router-mock.