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.push not causing page mount when going to same page with different query string

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
aulneaucommented, Sep 17, 2017

@arunoda @gragland I fixed my issues by adding a unique key to the component, it will cause it to remount 😃

2reactions
arunodacommented, Aug 30, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

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