State out of date when switching routes
See original GitHub issueExpected behavior
When attaching a callback to state.events.NAVIGATE
, and navigating to a new route, I expect state.route
to represent the current route.
Actual behavior
Instead, state.route
is out of date. It should displaying the current route, but instead shows the previous. For example, if navigating from a route called /cute
to one called /nice
, it will return /cute
.
function model (state, emitter) {
emitter.on(state.events.NAVIGATE, function () {
console.log(state.route)
})
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to keep state from refreshing while changing routes in ...
You can use context for that state is not lost every time the route changes. BUT when page is refreshed, state loses again...
Read more >Page state is not reset for navigating between dynamic routes
Bug report Describe the bug The page state is not reset for navigation between dynamic routes that served by the same source component....
Read more >Common Mistakes React Developers Make – And How to Fix ...
Don't Forget that Every Route Change Mounts and Unmounts a Component. Whenever you're using routing in a React application, you declare routes ......
Read more >React Router DOM: How to handle routing in web apps
Editor's note: This React Router DOM tutorial was last updated on 11 August 2021. It may still contain information that is out of...
Read more >Feature Overview v6.6.1 - React Router
Client side routing is enabled by creating a Router and ... React Router will cancel stale operations and only commit fresh data automatically....
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
Should be fixed in
choo@6.7.0
🎉Update: Turns out the below code is unreliable, events are picked up sporadically in Firefox and not at all in Safari. A better pattern for now is to just emit events which rely on current route/href from within views themselves.
I haven’t yet looked into why, and perhaps it’s a misunderstanding of the suggested flow above, but I have found that to get this to work 100%,
RENDER
needs to be explicitly triggered within theNAVIGATE
callback (def a hack!). I’ve added examples, view console to see behaviors.Explicit Render
The
RENDER
event is picked up afterNAVIGATE
because we force a re-renderDemo: https://choo-navigate-render-hack.glitch.me Code: https://glitch.com/edit/#!/choo-navigate-render-hack
Without Explicit Render
The
RENDER
event does not fire until the next tickDemo: https://choo-navigate-render.glitch.me Code: https://glitch.com/edit/#!/choo-navigate-render