get query object by Router.query empty in 9.2.1
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:41 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

If like me, you are using
getStaticProps, the docs state:This means you can watch
router.queryinuseEffectto check the params like so:Or watch for a specific param:
I just fixed the issue by changing my own code. I move the
router.queryrelated code from constructor to render function. It seems because when refreshing page, therouter.queryis empty initially. After I put it in the render function, it will re-render the page afterrouter.queryis updated.