DevMJ
DevMJ

Reputation: 351

this.props.history.push not working in react

I am trying to navigate using history push inside App.jsx. I have used below code for the same.

    componentDidMount() {
        if(!this.authService.isAuthenticated())
        {           
             this.props.history.push('/Login');
        }
    }

But it is giving an error as history is undefined. I tried logging props object and I got the below output.

enter image description here

Please help me in navigating inside app.jsx.

Upvotes: 0

Views: 1962

Answers (1)

Amruth
Amruth

Reputation: 5912

Export your component with withRouter.

import withRouter.

import { withRouter } from 'react-router'

Export in your component like this.

export default withRouter(MyComponent)//your component name 

Upvotes: 1

Related Questions