美文网首页
[React] - 当你在 this.props 中找不到 hi

[React] - 当你在 this.props 中找不到 hi

作者: iSakura | 来源:发表于2018-12-04 18:05 被阅读0次

React 中正常来说只有在 <Route> 包裹的组件中才能在 this.props 属性中找到 history 对象
但有些情况下我们就是希望在一个没被 <Route> 包裹的组件中用到 history 对象,比如:异步加载数据后跳转

网上有些方法提到可以在 class 前加上 @withRouter 的修饰

import { withRouter } from 'react-router-dom'

@withRouter
class ...

这种做法有时候会出现 Support for the experimental syntax 'decorators-legacy' isn't currently enabled 的报错
可以试着改成

export default withRouter(YourComponent)

或者(如果你还需要 connect

export default withRouter(connect(mapStateToProps)(YourComponent))

相关文章

网友评论

      本文标题:[React] - 当你在 this.props 中找不到 hi

      本文链接:https://www.haomeiwen.com/subject/absvcqtx.html