0

I tried using

<Redirect to = 'loginPage' />

but it is not redirecting me anywhere. what should i do?

Please help me

Anita
  • 256
  • 1
  • 6
  • 19

1 Answers1

0

You have two options to redirect:

Option 1:

Using <Redirect /> like in this example:

<Redirect
  to={{
    pathname: '/loginpage'
  }}
/>

This option works most of the times except for component lifecycle methods, such as ComponentDidMount where you need to opt for the second option below.

Option 2:

Using this.props.history.push("/loginpage"); like in this example:

  componentDidUpdate() {
    if (this.props.authenticated === false) {
      this.props.history.push("/loginpage");
    }
  }

Note that to use this option, the component must be a child component of <BrowserRouter />. Otherwise, you have to use the withRouter() HOC like in this example:

myComponent = withRouter(
  ServicePlanSelect
);
Igor Stetsiura
  • 775
  • 6
  • 14
James
  • 2,837
  • 6
  • 31
  • 67