0

My login application (react) and actual (react) application are in the same domain. My login application sets localStorage when user logs in successfully. In my actual application i have to check that value . if it is not there i have to redirect to login application in onload . What is the best practice to do that

Lakshmi Prasanna
  • 392
  • 2
  • 14

1 Answers1

0

You can check it in the componentDidMount life cycle method as below.

class LoggedInComponent extends Component {
  componentDidMount() {
    if (!localStorage.get('loggedIn')) {
      window.location = "/login";
      // You mentioned login app and actual app is deferent.
      // So you should use window.location
      // Otherwise you can use react-router's router.push('/login') function.
    }
  }
}

Use componentDidMount only. Because react v17 is going to deprecate the componentWillMount function.

Akhil P
  • 1,182
  • 2
  • 14
  • 24