react父组件调用子组件的路由跳转事件报错Cannot read property 'push' of undefined

摘要

近来在做react demo的时候碰到在my的父组件中调用login子组件的路由跳转事件Cannot read property ‘push’ of undefined
这是我的父组件 my

class my extends React.Component {
    constructor(props) {  //构造函数
        super(props);
    }
    render() {
        return (
            <div>
                <Login/>
            </div>
        );
    }
}

父组件中的login子组件内的去注册方法

goRegister() {
        this.props.history.push('/my/register')
    }

这样直接使用时报错

问题出在这里:在父组件调用子组件定义的跳转事件时,要传递history

<Login history={this.props.history}/>

你可能感兴趣的:(react父组件调用子组件的路由跳转事件报错Cannot read property 'push' of undefined)