How to redirect to another route programmatically
I have an <input /> with a handler on 'enter' key up event handler.
How can I redirect to /app?keyword=[input_value] in that event handler.
var transitionTo = Router.transitionTo;
transitionTo('your_route_name', query={keyword: input_value});
This works like a charm :clap: , thanks @hakanderyal . I should always RTFM https://github.com/rackt/react-router/blob/master/docs/api/Router.md#transitiontoroutenameorpath-params-query
Note this is different in v0.8.0, check out the upgrade guide:
https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md#07x---08x
Wondering how to get this to work with v0.12.0
@lesterzone Use Navigation mixin. https://github.com/rackt/react-router/blob/master/docs/api/mixins/Navigation.md#transitiontoroutenameorpath-params-query
Thanks @gaearon that's what I'm trying.
but it's A mixin for components
I want to router.redirectTo('home') after an ajax call on my Flux utils. Maybe I'm missing something.
@lesterzone https://github.com/rackt/react-router/blob/master/docs/guides/flux.md
Thanks again @gaearon I knew I was missing something. Just as reference:
// router.js
'use strict';
var _router;
module.exports = {
get: function() {
return _router;
},
set: function(router) {
_router = router;
}
};
// app.js
// a bunch of things here :D
var routes = require('./routes');
var RouterContainer = require('./router');
RouterContainer.set(Router.create({
routes: routes
}));
// someUtils.js
var router = require('../router');
router.get().transitionTo('home');
:tada: :tada:
@lesterzone I tried your approach, but after I successfully log in, I call:
router.get().transitionTo('account')
it sets the url like this with a hashbang:
http://localhost:5000/login#/account
as opposed to
http://localhost:5000/account
I'm not sure why, I'll like to share a snippet of code about my routes, maybe it can help you:
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="home" handler={Home}/>
<Route name="entities" handler={Entities}></Route>
<DefaultRoute handler={Home}/>
</Route>
);
Hm, yeah I have something similar. Thanks, I'll keep digging.
@captDaylight did you figure out a solution to the hashbang problem? I'm getting the same issue.
@captDaylight @01AutoMonkey I ran into this issue as well, and I was able to resolve it by using the router returned from Router.run instead of creating a separate router using Router.create.
Just pointing to the latest entry in the upgrade guide
https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md#navigation-mixin
@micouz how to navigate now? the link not valid
@regou https://github.com/rackt/react-router/blob/master/CHANGES.md#navigation-mixin
Anyone know the current doc for doing this? The latest link points to an obsolete hash tag.
@vevo-kurt-snyder here it is: https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#programmatic-navigation
please use GH search next time, before commenting on the outdated issues.
This is crazy, the documentation is terrible when describing something as basic as navigating programmatically. The link you post still doesn't make sense to me, my component gets a 'history' not a 'router' passed into its props (v2), so I still have no idea from the docs how to do basic navigation in my code.
If the documentation was better people wouldn't need to comment on old Github issues. So frustrating.
We have a very succinct guide on this: https://github.com/reactjs/react-router/blob/master/docs/guides/NavigatingOutsideOfComponents.md
The history prop, if you still receive it, is deprecated, as you should be able to see from the deprecation warnings you receive on attempting to use it.