location search or hash update cannot trigger page re-render
See original GitHub issueI 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
fromgatsby-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:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top 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 >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
I’d like to reopen this discussion. The design decision to not pass the updated
location
down toRoute
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 thehash
,search
, orstate
properties of alocation
; 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.@KyleAMathews sure, if i want to have different initial pages, i will directly use
pathname
as u suggested:pages/app/section-1.jsx
andpages/app/section-2.jsx
I wish to use 1 initial page
pages/app.jsx
because I only rely onsection-x
to do a little style tweak for the same contents. so i feelhash
is more straightforward.i may achieve it by
onRouteUpdate
ingatsby-browser
and i will also have a look ofcreating-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