React-router routing jump error using history.push

< H2 > react-router routing jump error using history.push < / H2 >
  • routing page http://localhost:8888/detail/5aae1cc8f5dfc27d7ad98909
  • Route form
<Route path="/detail/:id" component = {TopicDetail} key = "detail" />
<Route path="/user/login" component = {UserLogin} key = "login" />
  • component TopicDetail partial code
class TopicDetail extends React.Component{
    static contextTypes = {
        router:PropTypes.object,
    }
    constructor(props){
        super(props)
        this.goToLogin = this.goToLogin.bind(this)
    }
    /*--------------*/
    goToLogin() {
        this.context.router.history.push("user/login")
    }

    render() {
        return (
            <div>
                <section className={classes.notLoginButton}>
                    <Button raised color="inherit" onClick={this.goToLogin}>
                                     
                    </Button>
                </section>
            </div>
        )
    }
}
  • question: Click the button to call this.context.router.history.push ("user/login"). Why does the route jump from http://localhost:8888/detail/5aae1cc8f5dfc27d7ad98909 to http://localhost:8888/detail/user/login instead of http://localhost:8888/user/login?
Mar.02,2021

it's best to give the code Demo, so that it doesn't solve


see if there's anything wrong with the path, and add a "/". / "give it a try.


<Switch>
 <Router />
 <Router />
</Switch>
Menu