3

I want to use the alternate configuration of React Router: https://github.com/rackt/react-router/blob/1.0.x/docs/guides/basics/RouteConfiguration.md#alternate-configuration

However, I'm having trouble getting redirect to work. I tried this:

const routes = {
  childRoutes: [
    { path: '/test', component: Test },
  ],
  component: App,
  path: '/',
  redirect: {
    from: '/',
    to: '/test',
  },
};

As far as I could tell, there is no documentation for this. Does the functionality exist? How should it be done?

1 Answer 1

1

Comparing the sample code in the link you posted, with the sample above (in Preserving URLs), I think both samples refer to setting the same routes (the only difference probably is that the last one uses the Alternate Configuration). And we can infer that the current way of making redirects is using the onEnter function.

For instance, if you want this (using JSX):

<Redirect from="messages/:id" to="/messages/:id" />

And you use the Alternate Configuration, you'll have to write it like this:

{ path: 'messages/:id',
  onEnter: function (nextState, replaceState) {
    replaceState(null, '/messages/' + nextState.params.id)
  }
}

Update: Your particular case is special, because you want to redirect from /, so you might want to try using IndexRedirect or indexroute.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Protron. This didn't work exactly but using onEnter got me thinking. Here's how I solved it: onEnter: (nextState, transition) => { if (nextState.location.pathname === '/') { transition.to('/test', null); } },
@Chris Nice! But in case you are interested in finding other solutions you might want to take a look at the update in my answer.
transition isn't one of the arguments coming into the function for me. I'm having the same issue trying to redirect / to something else along with a bunch of others. This is much more complicated than <Redirect>, but then you can do require.ensure :/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.