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.

get query object by Router.query empty in 9.2.1

See original GitHub issue

Bug report

get query object by Router.query empty in 9.2.1

Describe the bug

when i use dynamic-routing, i come across that bug, I would get query object by Router.query in componentDidMount func; like this public componentDidMount() { console.log(‘Router.query’, Router.query); } the query is empty

To Reproduce

Go to example/dynamic-routing modify the comment.js like as:

import Router from 'next/router';
class Comment extends React.Component<{}, {}> {
    public componentDidMount() {
       console.log('Router.query', Router.query);
    }
    public render() {
      return (
        <>
          <h1>Post</h1>
        </>
      );
    }
  }

Expected behavior

get query object by Router.query

System information

  • OS: macOS
  • Browser : chrome
  • Version of Next.js: 9.2.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:41 (7 by maintainers)

github_iconTop GitHub Comments

39reactions
minlarecommented, Oct 14, 2020

If like me, you are using getStaticProps, the docs state:

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.

This means you can watch router.query in useEffect to check the params like so:

const router = useRouter();
useEffect(() => {
  console.log(router.query);
}, [router.query]);

Or watch for a specific param:

const router = useRouter();
useEffect(() => {
  console.log(router.query.myParam);
}, [router.query.myParam]);
12reactions
ivanhoe-devcommented, Feb 15, 2020

I just fixed the issue by changing my own code. I move the router.query related code from constructor to render function. It seems because when refreshing page, the router.query is empty initially. After I put it in the render function, it will re-render the page after router.query is updated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why would withRouter router.query be empty on first render in ...
I move the router.query related code from constructor to render function. It seems because when refreshing page, the router.query is empty ...
Read more >
[Solution] NextJS Router Query Not Working in useEffect
If you ever tried to get query params from next/router in useEffect , you probably ... route parameters provided, i.e query will be...
Read more >
vue router clear query | The AI Search Engine You Control
For some reason router.replace() & router.push() need a non-empty object as query. So all rest to do is clean your initial query object...
Read more >
Spring Data Elasticsearch - Reference Documentation
USE_DECLARED_QUERY tries to find a declared query and throws an exception if it cannot find one. The query can be defined by an...
Read more >
Fixing Next.js router query param returning undefined on ...
If you're getting undefined query params in nextjs on the first ... route parameters provided, i.e query will be an empty object (...
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