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.

Router query empty in componentDidMount

See original GitHub issue

Bug report

Describe the bug

The docs state: The query object will be empty during prerendering if the page is statically optimized. but I am also seeing the query object empty in the componentDidMount lifecycle method.

To Reproduce

  1. Clone https://github.com/zlwaterfield/next-router-query-test
  2. Run yarn dev
  3. Go to http://localhost:3004/router-test?test=test
  4. Notice the query object is empty but the document.location.search is correct and the query shows in asPath.

Expected behavior

The query object should be

{
  test: 'test'
}

Screenshots

Screen Shot 2020-05-16 at 5 04 30 PM

System information

  • Version of Next.js: 9.4.0

Additional context

I understand the query object would be empty in the prerender but should it not show when the component is mounted?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zlwaterfieldcommented, May 19, 2020

Ah, I found this:

Pages that are statically optimized by Automatic Static Optimization will be hydrated without their route parameters provided, i.e query will be an empty object ({}).

After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.

I will confirm this and then look into making a PR to make it more clear in the docs. Thanks for the help.

0reactions
balazsorban44commented, Jan 29, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter/withRouter receive undefined on query in first render
During prerendering stage of the component, the query object of the router will be empty. isReady: boolean - Whether the router fields are ......
Read more >
next/router | Next.js
query : Object - The query string parsed to an object, including dynamic route parameters. It will be an empty object during prerendering...
Read more >
Fixing Next.js router query param returning undefined on ...
First, let's just try to reproduce this issue. You might encounter some cases where you need access to query params of a route....
Read more >
How to Search and Filter Components in React - freeCodeCamp
How to Build the Search Component. Inside our APP function we use the useState() hooks to set the query q to an empty...
Read more >
Using Suspense and React Query: Tutorial with examples
If you need to fetch data from a source, for example, an API, you usually need to create a request in the useEffect()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found