A
A
Andrei Smirnov2016-11-18 12:52:23
React
Andrei Smirnov, 2016-11-18 12:52:23

How to do redirect with react-router without JSX?

There is a router configuration described as a JS object (not JSX).
Each element of the configuration array is essentially a Route object.
There is a path /account and its child paths: /account/profile, /account/password, etc.
I would like: so that when you go to /account, /account/profile is automatically included.
If I had used JSX I would have inserted a Redirect object, but JSX is not here because Lazy loading of components is needed.

...
        {
          path: 'account',
          component: /* ... */,
          getChildRoutes(partialNextState, cb) {
            return cb(null, [
              {
                path: 'profile',
                component: /* ... */,
              },
            ]

I tried adding an onEnter handler:
onEnter: (_, replace) => { if (_.location.pathname === '/account') { replace('/account/profile') } },

and it works but looks like a hack. Is there a correct way to solve this?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Gromov, 2016-11-18
@pinebit

https://github.com/ReactTraining/react-router/pull/1736

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question