react-router icon indicating copy to clipboard operation
react-router copied to clipboard

TransitionTo flickering with HashLocation

Open mattydoincode opened this issue 11 years ago • 2 comments

Hi Friends,

I'm trying to get a handle on using this library, as I think it's a great way to organize things, however I'm having trouble using TransitionTo with Location.HashLocation in a Flux architecture. I'm building a simple login page that at this point has two routes, one is default with a path of /, and the other is called "forgotpassword" with a path of /forgot.

Issue

When I use the transitionTo method in an attempt to navigate from my defaultRoute to my forgot password route, the URL simply flickers for a split second.

Expected

The route would go from /#/ to /#/forgot and my new component would render

Actual

The route goes from /#/ to /#/forgot and then back to /#/ almost instantaneously but I can see the flicker every other time. The new component does not render.

Relevant Note

When I change my router to use HistoryLocation instead of HashLocation the route makes the jump, and the new component is rendered, however the route goes from / to /forgot# with the hashtag at the end, which when hitting the back button, takes two clicks to go back. As in it goes from / -> click on link -> /forgot# -> click back -> /forgot -> click back -> /

Relevant Code

module.exports = (
  <Route name="app" path="/" handler={Login}>
    <Route name="forgotpassword" path="forgot" handler={ForgotPassword}/>
    <DefaultRoute handler={BasicLogin}/>
  </Route>
);

I have a click handler in my BasicLogin component that does the following:

forgotPassword: function () {
    router.transitionTo('forgotpassword');
},

and I'm using a router pattern that has been recommended in other posts on this github which is what router refers to in code sample above

'use strict';

var router;

// Here's the trick: assign to module.exports before any require()s
module.exports = {
  getCurrentPath() {
    return router.getCurrentPath();
  },

  makePath(to, params, query) {
    return router.makePath(to, params, query);
  },

  makeHref(to, params, query) {
    return router.makeHref(to, params, query);
  },

  transitionTo(to, params, query) {
    router.transitionTo(to, params, query);
  },

  replaceWith(to, params, query) {
    router.replaceWith(to, params, query);
  },

  goBack() {
    router.goBack();
  },

  run(render) {
    router.run(render);
  }
};

var routes = require('./routes'),
    Router = require('react-router');

router = Router.create({
  routes: routes,
  location: Router.HashLocation
});

My full source is here, it's actually quite small at this point and pretty well organized so wouldn't be too much of a rabbit hole. https://github.com/mattydoincode/matt-react/tree/master/source/scripts

Any and all help is greatly appreciated!!

mattydoincode avatar Mar 05 '15 15:03 mattydoincode

I think that's because you're using basic <a> anchors in addition to invoking transitionTo, i.e. you're triggering two transitions on a click. Use Link instead. That way you don't even need to call transitionTo yourself. Otherwise, you'd need to prevent the transition caused by the anchor (event.preventDefault).

agundermann avatar Mar 05 '15 16:03 agundermann

Jeeeeez this was exactly the problem. I wasn't looking in the right places at all. Thanks for seeing the simple issue in all of this!! Everything works grand now.

mattydoincode avatar Mar 05 '15 23:03 mattydoincode