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

router.transitionTo no longer works

Open ivan-kleshnin opened this issue 11 years ago • 12 comments

When I add mixins: [..., Router.Navigation] to component, this.transitionTo(...) successfully redirects to required path. However when I access same method from imported router instance (router.transitionTo(...)) – it just reloads the page and stays on the same URL. (I do not call deprecated static method Router.transitionTo, its instance method!)

Documentation states it's correct syntax: https://github.com/rackt/react-router/blob/0e1dfefbc201cb7849a66e12590823b18ef1d3a3/docs/api/create.md https://github.com/rackt/react-router/blob/3e6a1314490857b325d5ae5ea7383b081c256b02/docs/guides/flux.md

Maybe its already deprecated but yet undocumented? I'd prefer to call this method from router, same code-length, less magic.

ivan-kleshnin avatar Jan 16 '15 13:01 ivan-kleshnin

It's not deprecated, this is really handy for Flux apps. Do you mind showing the code? It certainly works for me.

gaearon avatar Jan 16 '15 13:01 gaearon

Thank you. Please refer to: https://github.com/Paqmind/twitto/tree/master/src/app_react

Root folder is total mess, but linked one should be easy to grasp. I basically try to rewrite ampersand demo app (https://github.com/HenrikJoreteg/humanjs-sample-app) on React. I run this with $ gulp dev + $ 6to5-node bin/www.

I can clean it up even more if required.

ivan-kleshnin avatar Jan 16 '15 14:01 ivan-kleshnin

Not sure how this would cause a page refresh, but at first glance it seems to me there's a circular dependency between the router and components. See #575. In my opinion the cleanest way would be to pass the instance through <RouteHandler />

agundermann avatar Jan 16 '15 15:01 agundermann

Circular dependency does exist but there is surely yet another, more involved problem:

require('../router').transitionTo('robots-edit', {id: this.getParams().id});
// OR
this.transitionTo('robots-edit', {id: this.getParams().id});

Both works but page refreshes two times in both cases!

I think noone noticed this behavior yet, because links are meant to be built upon Link component. And I wanted to experiment with raw behaviour. I'll attach link to working demo instance as soon as I can.

ivan-kleshnin avatar Jan 16 '15 15:01 ivan-kleshnin

How do I run your project? gulp dev gives me

/Users/dan/Documents/Projects/twitto/gulpfile.js:5
let faker = require("faker");
    ^^^^^
SyntaxError: Unexpected identifier

I don't know how to enable 6to5 for a Gulpfile..

gaearon avatar Jan 16 '15 22:01 gaearon

I just changed shebang from #!/usr/bin/env node to #!/usr/bin/env 6to5-node in gulp.js file. In OSX it's located at /usr/local/lib/node_modules/gulp/bin/gulp.js You may find it with commands like:

$ which gulp | readlink -f (Linux)
$ which gulp | xargs greadlink -f (OSX, requires $ brew install coreutils)

Arguably better was to add another gulp6 command but that is kinda offtopic.

My motivation to use 6to5 right now is that io.js is already released (with support for es6 syntax), in a few weeks it should be production ready.

Sorry for inconvinience.

ivan-kleshnin avatar Jan 17 '15 07:01 ivan-kleshnin

Thanks. I'm all for using 6to5 (or io for that matter), just didn't know the details!

I'll give it a go on Monday.

gaearon avatar Jan 17 '15 09:01 gaearon

@gaearon there is yet another potential trap here which I should mention. Gulp depends on vinyl-fs > glob-watcher > gaze > globule > lodash where last element is broken outdated lodash version. I raised a corresponding issue but glob-watcher authors don't want to update their gaze dependency version for some reason.

So to run all this you'll need to remove this folder: <project>/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/node_modules/globule/node_modules/lodash

Then globule will look for lodash in <project>/node_modules dir and everything should run.

ivan-kleshnin avatar Jan 17 '15 13:01 ivan-kleshnin

All described was just an unexpected (unusual) reaction for another issues (like circular dependency, not preventing default event action). So it's probably dont worth time to investigate. I would only add to documentation a mention about possilbe circular dependency between router and components.

ivan-kleshnin avatar Jan 20 '15 09:01 ivan-kleshnin

Pardon, I haven't had the time to take a look yet :-( Agreed re: circular dependencies, here's what I do btw:

'use strict';

var router;

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((Handler, state) => {
      render(Handler, state);
    });
  }
};

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

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

I just remembered I had the same (or similar) problem.

gaearon avatar Jan 20 '15 11:01 gaearon

Thanks!

ivan-kleshnin avatar Jan 20 '15 12:01 ivan-kleshnin

this bit me too and cost some time

above solution to create the router instance solved it

it might help others to mention above solution in the docs

thanks for this great tool!

barbalex avatar Jun 12 '15 14:06 barbalex