jgx
jgx

Reputation: 99

Using indexRedirect in a route configuration object

I was wondering if it's possible to use "indexRedirect" in a route configuration object (so without using JSX).

I tried without success, so I assumed that this isn't supported at the moment, but then my issue (https://github.com/reactjs/react-router/issues/3150) was closed without comment, so I still don't know if it's because the feature is already there but I misuse it, or because this is a unwanted feature.

Thanks in advance. :)

Upvotes: 1

Views: 1873

Answers (3)

Mono
Mono

Reputation: 392

As described in the docs, the equivalent to indexRedirect using a plain configuration object looks like this

const routes = [{
  path: '/',
  component: App,
  indexRoute: { onEnter: (nextState, replace) => replace('/welcome') },
  childRoutes: [
    { path: 'welcome', component: Welcome },
    { path: 'about', component: About }
  ]
}]

Upvotes: 2

Otto Kovarik
Otto Kovarik

Reputation: 1

I would appreciate the same thing as JGX: working relative paths for this kind of redirecting

Upvotes: 0

knowbody
knowbody

Reputation: 8276

Using plain routes config it's a bit more low level API, and there is no point of having special redirect or indexRedirect keys in the routes object. Instead you could use onEnter to accomplish a redirect. Here is how:

import React from 'react'
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router'

const Home = () => <h3>Home</h3>

const About = () => <h3>About</h3>

const routes = [{
  path: '/',
  component: Home,
  onEnter: (nextState, replace) => replace('/about')
}, {
  path: '/about',
  component: About
}]

render(
  <Router history={browserHistory} routes={routes} />,
  document.getElementById('root')
);

Edit: added this into docs as well.

Upvotes: 0

Related Questions