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.

State out of date when switching routes

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
yoshuawuytscommented, Jan 17, 2018

Should be fixed in choo@6.7.0 🎉

0reactions
jongacnikcommented, Oct 18, 2017

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 the NAVIGATE callback (def a hack!). I’ve added examples, view console to see behaviors.

Explicit Render

The RENDER event is picked up after NAVIGATE because we force a re-render

emitter.prependListener(state.events.NAVIGATE, function () {
  setTimeout(function () {
    emitter.emit(state.events.RENDER)
  })
  emitter.once(state.events.RENDER, function () {
    console.log(state.route)
  })
})

Demo: 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 tick

emitter.prependListener(state.events.NAVIGATE, function () {
  emitter.once(state.events.RENDER, function () {
    console.log(state.route)
  })
})

Demo: https://choo-navigate-render.glitch.me Code: https://glitch.com/edit/#!/choo-navigate-render

Read more comments on GitHub >

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

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