React Router v4 Redirecting on form submit

后端 未结 2 977
轻奢々
轻奢々 2020-12-10 02:07

Brand new to react, Attempting to redirect to a \"homepage\" after clicking submit on a \"login\" page. Tried to follow the react router tutorial.

When I click the s

相关标签:
2条回答
  • You have to return Redirect in render method (otherwise it will not be rendered and as a result redirection will not happen):

      render() {
            const redirectToReferrer = this.state.redirectToReferrer;
            if (redirectToReferrer) {
                return <Redirect to="/home" />
            }
            // ... rest of render method code
       }
    
    0 讨论(0)
  • 2020-12-10 02:42

    I am now able to redirect to the "homepage" by swapping out <Redirect to="/home" /> with this.props.history.push("/home"); using withRouter. Not sure why the first way would not work though.

    0 讨论(0)
提交回复
热议问题