Ad

Redux How To Redirect On ComponentWillMount Using React-router

- 1 answer

how to redirect on componentWillMount using react-router

export class NewStock extends React.Component {
  constructor(props, context) {
    super(props, context);
  }

  componentWillMount() {
    const { session, dispatch, router } = this.props
    if (session.userSessionData.get('logged_in') == false) {
      router.transitionTo('/login')
    }
  };

this code:

router.transitionTo('/login')

only return:

Uncaught TypeError: router.transitionTo is not a function
Ad

Answer

Try this.props.history:

const { history } = this.props
history.pushState(null, '/login') or
history.replaceState(null, '/login')

If your module isn't a direct descendant of the router you can still access this prop by wrapping your component in the withRouter(YourComponent) HOC (in v4).

Ad
source: stackoverflow.com
Ad