GithubHelp home page GithubHelp logo

Comments (3)

scottrippey avatar scottrippey commented on August 16, 2024

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.

scottrippey avatar scottrippey commented on August 16, 2024

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.

raon0211 avatar raon0211 commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.