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.

Add a `ready: boolean` to `Router` returned by `useRouter`

See original GitHub issue

Because of the way Next.js does initial render of the pages, and that the initial render on client-side and server-side has to be the same (see https://reactjs.org/docs/react-dom.html#hydrate ), sometimes when a page is rendered, its’ query (returned i.e. by useRouter() hook) will be empty, even though it actually has query params. After the initial render, soon after there will be another render in which the query will be then populated with proper query params. ( Example described also in i.e. https://github.com/zeit/next.js/issues/7880 )

My code relies on those query params very heavily and we need to know “are those the real current query params or not yet?”. (i.e. because if query params are missing or incorrect, we’d like to redirect the user to another page)

Describe the solution you’d like

I think that either:

  • query should be always populated with the current query params (even on initial client-side hydration that happens after a server-side render)
  • or, (IMO better), add a ready: boolean flag to Router, so the component can listen to this using i.e. useEffect and ignore the query params when they aren’t parsed yet.

Describe alternatives you’ve considered

I think we’ll have to go with some setTimeout or sth for now until a better solution is found.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:81
  • Comments:53 (23 by maintainers)

github_iconTop GitHub Comments

22reactions
defxcommented, Oct 1, 2019

+1 for better documentation around static patterns.

20reactions
lifeiscontentcommented, Apr 18, 2020

@jtomaszewski would you ever want to use the router when it is not “ready”? I feel like next.js should only render a page once the router is ready, that way I don’t have to do a bunch of conditional logic around if query params are loaded or not.

like in this example:

https://github.com/lifeiscontent/realworld/blob/2feda02ec18d617b4806dad071d8a97908f52e0d/web/src/containers/index-page/index.js#L14-L25

to me, it’d be ideal if next would just wait for the params to be ready before rendering, that way I don’t need to conditionally check if they’re “ready” which seems like an implementation detail to me that shouldn’t be exposed to us as the users of next.js

What are your thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter/withRouter receive undefined on query in first render
I found something: isReady: boolean - Whether the router fields are updated client-side and ready for use. Should only be used inside of ......
Read more >
next/router | Next.js
isReady : boolean - Whether the router fields are updated client-side and ready for use. Should only be used inside of useEffect methods...
Read more >
Bountysource
Add a `ready : boolean` to `Router` returned by `useRouter`
Read more >
Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in ... If no data can be found, we return this boolean...
Read more >
Fixing Next.js router query param returning undefined on ...
Fix. useRouter hook returns isReady field which is a boolean value that denotes if the router fields are updated client-side and ready for ......
Read more >

github_iconTop Related Medium Post

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 Hashnode Post

No results found