Navigating Programmatically in React-Router v4

后端 未结 10 1186
后悔当初
后悔当初 2020-12-02 11:16

I couldn\'t wait and I jumped into using the latest alpha version of react-router v4. The all-new is great in keeping your U

10条回答
  •  醉酒成梦
    2020-12-02 11:52

    I found using state, a ternary operator and worked best. I think this is also the prefered way since it is closest to the way v4 is set up.

    In the constructor()

    this.state = {
        redirectTo: null
    } 
    this.clickhandler = this.clickhandler.bind(this);
    

    In the render()

    render(){
        return (
            
    { this.state.redirectTo ? : (
    ..
    ) }

    In the clickhandler()

     this.setState({ redirectTo: '/path/some/where' });
    

    Hope it helps. Let me know.

提交回复
热议问题