简体   繁体   中英

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.

在此处输入图片说明

Please help me in navigating inside app.jsx.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM