STRML / react-router-component

Declarative router component for React.
http://strml.viewdocs.io/react-router-component
MIT License
873 stars 94 forks source link

onBeforeNavigation is invoked late #204

Closed jsg2021 closed 6 years ago

jsg2021 commented 6 years ago

I just noticed that this call is made AFTER the browser is moved to the new route... with no opportunity to cancel... which kinda invalidates its usefulness.

Environment.js:

Environment.prototype.setPath = function(path, navigation, cb) {
  // Support (path, cb) arity.
  if (typeof navigation === 'function' && cb === undefined) {
    cb = navigation;
    navigation = {};
  }
  // Support (path) arity.
  if (!navigation) navigation = {};

  if (!navigation.isPopState) {
    if (navigation.replace) {
      this.replaceState(path, navigation);
    } else {
      this.pushState(path, navigation);
    }
  }
  this.path = path;
  this.notify(navigation, cb); // <--- setPath on routers is called (and subsequently on(Before)Navigation calls
};

To allow for aborting in onBeforeNavigation, it would have to be called before replace/pushState calls...

jsg2021 commented 6 years ago

I'm going to close this, because I don't think it can be fixed with the current architecture of this library.