摘要
近来在做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'。通过具体代码示例,详细解释了如何正确传递history属性以实现子组件的路由跳转功能。

876

被折叠的 条评论
为什么被折叠?



