Kris MP Kris MP - 3 months ago 155
React JSX Question

redux how to redirect on componentWillMount using react-router

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

Answer

Try this.props.history:

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