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

How to redirect to another route programmatically

Open camsong opened this issue 12 years ago • 27 comments

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.

camsong avatar Sep 24 '14 09:09 camsong

var transitionTo = Router.transitionTo;

transitionTo('your_route_name', query={keyword: input_value});

hakanderyal avatar Sep 24 '14 16:09 hakanderyal

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

camsong avatar Sep 25 '14 04:09 camsong

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

ryanflorence avatar Oct 04 '14 20:10 ryanflorence

Wondering how to get this to work with v0.12.0

lesterzone avatar Feb 11 '15 21:02 lesterzone

@lesterzone Use Navigation mixin. https://github.com/rackt/react-router/blob/master/docs/api/mixins/Navigation.md#transitiontoroutenameorpath-params-query

gaearon avatar Feb 11 '15 21:02 gaearon

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 avatar Feb 11 '15 21:02 lesterzone

@lesterzone https://github.com/rackt/react-router/blob/master/docs/guides/flux.md

gaearon avatar Feb 11 '15 21:02 gaearon

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 avatar Feb 11 '15 23:02 lesterzone

@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

captDaylight avatar Mar 17 '15 15:03 captDaylight

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>
);

lesterzone avatar Mar 17 '15 16:03 lesterzone

Hm, yeah I have something similar. Thanks, I'll keep digging.

captDaylight avatar Mar 17 '15 16:03 captDaylight

@captDaylight did you figure out a solution to the hashbang problem? I'm getting the same issue.

map01ASCIIDOC10 avatar Mar 31 '15 15:03 map01ASCIIDOC10

@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.

ElsewhereGames avatar May 04 '15 19:05 ElsewhereGames

Just pointing to the latest entry in the upgrade guide

https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md#navigation-mixin

couzic avatar Sep 26 '15 21:09 couzic

@micouz how to navigate now? the link not valid

regou avatar Nov 27 '15 06:11 regou

@regou https://github.com/rackt/react-router/blob/master/CHANGES.md#navigation-mixin

knowbody avatar Nov 30 '15 13:11 knowbody

Anyone know the current doc for doing this? The latest link points to an obsolete hash tag.

vevo-kurt-snyder avatar Mar 02 '16 01:03 vevo-kurt-snyder

@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.

knowbody avatar Mar 02 '16 10:03 knowbody

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.

davedx avatar May 20 '16 07:05 davedx

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.

taion avatar May 20 '16 14:05 taion