React-router: How do I call 'replace' from within `getComponent`?

Created on 26 Oct 2016  路  3Comments  路  Source: ReactTraining/react-router

I can't understand why there is no replace method in getComponent.

My usecase is as follows. In positive case I need a redirect to different route, in negative case I need to render a component in place:

if true, / =redirect=> /green
if false, render Red right here /

<Route path="" component={App}>
    <Route path="/" getComponent={(nextState, cb) => {
            if(true) {
                replace('green') // but there is no replace() here in getComponent(). Why?
                cb(null);
            }
            else {
                cb(null, Red)
            }
        }}>

    </Route>
</Route>

How to achieve it? You provide two methods, onEnter and getComponent. But neither solves this use case. How does react-router suppose to solve this typical use case?

Most helpful comment

Because that is not the correct place to be redirecting. Use an onEnter hook for this.

All 3 comments

Because that is not the correct place to be redirecting. Use an onEnter hook for this.

How did you identify it is not a right place? I've just provided you with an example how a right place it is.

  1. Does the plugin provide reasonable means to have replace function in getComponent function?
  2. If not, what are plugin's best practices to achieve it?

Use an onEnter hook for this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maier-stefan picture maier-stefan  路  3Comments

yormi picture yormi  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

jzimmek picture jzimmek  路  3Comments

davetgreen picture davetgreen  路  3Comments