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.

location search or hash update cannot trigger page re-render

See original GitHub issue

I noticed location search or hash updates without pathname change won’t trigger the page re-render.

I used 3 different ways to update the location from /app to /app?abc=def#test

  • navigateTo
  • Link from gatsby-link
  • this.props.history.push

and I cannot have any updated props.location in layout/index.jsx or page/app.jsx

after a little research, I found the issue is in https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/component-renderer.js#L42 the ComponentRenderer will only save new location with changed pathname into the new state so that shouldComponentUpdate can return true

Is this expected? Any reasons to ignore location search or hash change?

I can simply fix it by adding another else if case to check other location attributes, like

if (this.state.location.pathname !== nextProps.location.pathname) {
  // same here
} else if (this.state.location.key !== nextProps.location.key) {
  this.setState({
     location: nextProps.location
  })
}

Issue Analytics

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

github_iconTop GitHub Comments

8reactions
lourdcommented, Jan 10, 2018

I’d like to reopen this discussion. The design decision to not pass the updated location down to Route components inhibits the ability to do some client-side state toggling correctly; think widgets that have toggled states — navigation menus, modals, etc. Those should still work with the browser back and forward buttons to maintain fundamental browser UX.

By not updating the location to Route components when the pathname doesn’t change, there’s no way to toggle elements using URL-related state. You can’t use the hash, search, or state properties of a location; none of them cause the page to re-render on the client currently.

My current use case is that I want a nav menu to open with a hash of #nav. It could be at /#nav, or /foo#nav. Not seeing any way to do this without getting into the internals. The client-side routes section of the docs is for a different use case.

0reactions
aobo-ycommented, Dec 25, 2017

@KyleAMathews sure, if i want to have different initial pages, i will directly use pathname as u suggested: pages/app/section-1.jsx and pages/app/section-2.jsx

I wish to use 1 initial page pages/app.jsx because I only rely on section-x to do a little style tweak for the same contents. so i feel hash is more straightforward.

i may achieve it by onRouteUpdate in gatsby-browser and i will also have a look of creating-client-only-routes

but still, I am wondering if there are any real drawbacks to allow re-render page when hash changes. Thanks for your help

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to re-render component after update hash correctly with ...
I'm use he next code for update state: const locationHashChanged = () => { if (window.location.hash === "#state-active") ...
Read more >
location.reload() - Web APIs | MDN
The location.reload() method reloads the current URL, like the Refresh button. The reload may be blocked and a SECURITY_ERROR DOMException ...
Read more >
Fixing the 'cannot GET /URL' error on refresh with React ...
In this post you'll learn how to fix the 'cannot GET /URL' error with React Router. Along the way, you'll also learn how...
Read more >
How do I use JavaScript to modify the URL without reloading ...
Bear in mind that the History API only allows same-origin URLs, so you cannot navigate to an entirely different website. Using the Location...
Read more >
Location reload() Method - W3Schools
Definition and Usage. The reload() method reloads the current document. The reload() method does the same as the reload button in your browser...
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