Skip to content

onEnter's replace not working inside match #4051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
agusputra opened this issue Oct 15, 2016 · 3 comments
Closed

onEnter's replace not working inside match #4051

agusputra opened this issue Oct 15, 2016 · 3 comments

Comments

@agusputra
Copy link

Try this code and navigate to /route2. Then replace not work and there is some errors in browser console.

const routes = (
    <Route path='/'>
        <Route path='route1' onEnter={() => console.log('route1')}/>
        <Route path='route2' onEnter={(state, replace) => {            
            console.log('route2')
            // It is not work and raise error in browser console
            replace('/route1')
        }}/>
    </Route>
)
match({history: browserHistory, routes}, (err, redirect, props) => {
    const router = (
        <Router {...props}/>
    );
    ReactDOM.render(router, app)
});

But this code works (without match).

const routes = (
    <Route path='/'>
        <Route path='route1' onEnter={() => console.log('route1')}/>
        <Route path='route2' onEnter={(state, replace) => {
            console.log('route2')
            replace('/route1')
        }}/>
    </Route>
)
const router = (
    <Router history={browserHistory} routes={routes}/>
)
ReactDOM.render(router, app)

I'm using react-router v2.8.1

@agusputra agusputra changed the title onEnter's replace notworking inside match onEnter's replace not working inside match Oct 15, 2016
@timdorr
Copy link
Member

timdorr commented Oct 15, 2016

You left out the critical part: What is the error you got?

@agusputra
Copy link
Author

agusputra commented Oct 16, 2016

With above code, when I enter /route2 in browser then I got

Warning: [react-router] `Router` no longer defaults the history prop to hash history. Please use the `hashHistory` singleton instead. http://tiny.cc/router-defaulthistory
Warning: [react-router] Location "/" did not match any routes

@timdorr
Copy link
Member

timdorr commented Oct 17, 2016

You don't have any async routes, so you shouldn't use match on the client side. Use a plain <Router>.

@timdorr timdorr closed this as completed Oct 17, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants