Router.push not causing page mount when going to same page with different query string
See original GitHub issueWhen using Router.push
to route to the same page but with a different query string value the page is not remounted. Instead, both getInitialProps
and componentWillReceiveProps
are executed. Is this the intended behavior?
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
I would expect the current page component to unmount (componentWillUnmount
should fire) and the new page to mount (componentDidMount
should fire). If people want to shallow route they can use the Router shallow:true
option.
Current Behavior
Steps to Reproduce (for bugs)
import { Component } from 'react';
import Router from 'next/router';
class PageTest extends Component {
constructor(props) {
super(props);
this.state = {};
this.goToPage = this.goToPage.bind(this);
}
static async getInitialProps({ query }){
console.log('getInitialProps');
return {
pageId: query.pageId
}
}
componentWillReceiveProps(nextProps){
console.log('componentWillReceiveProps', nextProps);
}
componentDidMount(){
console.log('componentDidMount');
}
componentWillUnmount(){
console.log('componentWillUnmount');
}
goToPage(e){
e.preventDefault();
const { pageId } = this.props;
const nextPageId = pageId === 'a' ? 'b' : 'a';
Router.push(
`/page-test?pageId=${nextPageId}`,
`/page-test/${nextPageId}`,
//{ shallow: false } // Doesn't do anything
);
}
render() {
return (
<div>
<a href="#" onClick={this.goToPage}>Go to other page</a>
</div>
);
}
}
export default PageTest;
Context
Your Environment
Tech | Version |
---|---|
next | 3.0.6 |
node | 8.1.4 |
OS | OSX |
browser | Chrome |
etc |
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
How to use router.push for same path, different query ...
That's because it's not different from your current path. If the value of type is different it won't give you NavigationDuplicated error.
Read more >Shallow Routing - Next.js
When shallow routing is used with middleware it will not ensure the new page matches the current page like previously done without middleware....
Read more >vue router push with query params - You.com | The AI Search ...
After updating vue-router from 3.4.1 to 3.4.2 a router.push() with only query params causes navigation to an empty path. Clicking the "Push query" ......
Read more >Waiting for the result of a Navigation - Vue Router
When using router-link , Vue Router calls router.push to trigger a navigation. ... page, there are a few situations where users will remain...
Read more >VueJS: $router.push not working with query parameters-Vue.js
By changing the approach component data can be updated as per the new query string value. Here is how it can be done....
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 FreeTop 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
Top GitHub Comments
@arunoda @gragland I fixed my issues by adding a unique
key
to the component, it will cause it to remount 😃This is not a bad thing. This is a feature. This is the only you could do animations and changes view without re-rendering the page.