解决:react router Cannot read properties of undefined (reading 'push')

最近在学习react,使用router过程中在子组件想进行路由跳转,使用this.props.history报错,进一步调试发现this.props为undefined,查阅文档得知

没办法通过 this.props.children 取得当前组件的子元素。 因为this.props.children 返回的是组件拥有者传递给你的 passed onto you 子节点。

解决方法:(withRouter)

使用\color{red}{withRouter}包裹当前组件

import React, { Component } from "react";
import { withRouter } from "react-router-dom";

class Documentation extends Component {
  state = {};
  render() {}
}
export default withRouter(Documentation);

carbon (4).png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容