Skip to content

Checking if state is still active #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
agundermann opened this issue Jan 11, 2015 · 1 comment
Closed

Checking if state is still active #674

agundermann opened this issue Jan 11, 2015 · 1 comment

Comments

@agundermann
Copy link
Contributor

The async-data example suggests the following approach for fetching data async before render:

Router.run(routes, function (Handler, state) {
  fetchData(state.routes, state.params).then((data) => {
    React.render(<Handler data={data}/>, document.getElementById('example'));
  });
});

The problem I see with this is that (unless the promise rejects) React.render will always be called, even if a new state came in the meantime. And if that newer state was already rendered, the UI will end up in an inconsistent state (browser URL and internal router state will differ from the manually set props). If you click fast enough in the example, you may end up seeing a different profile than the one you clicked last which is also reflected in the current URL.

This doesn't only affect async rendering, but can also happen when transitioning before a render:

Router.run(routes, function (Handler, state) {
  this.transitionTo(..); 
  // state is now stale and not consistent with the router state / URL
  React.render(<Handler data={getDataFromState(state)}/>, document.getElementById('example'));
});

A solution to this would be to keep track of the active state yourself:

var activeState = null;
Router.run(routes, function (Handler, state) {
  activeState = state;
  fetchData(state.routes, state.params).then((data) => {
    if(activeState === state){
      React.render(<Handler data={data}/>, document.getElementById('example'));
    }
  });
});

So I think that either this approach should be promoted, or the router should provide a way to check if state is still active.

@ryanflorence
Copy link
Member

Yeah, should probably call that out. If you could send a PR with that change I'd merge it immediately, otherwise I'll do it next time I'm in the docs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants