【发布时间】:2018-08-13 08:09:31
【问题描述】:
我知道标题可能有点混乱。这是一个代码示例:
//First.js
export default class First extends React.Component {
constructor(props) {
super(props);
module.exports.push = route => {
this.refs.router.push(route)
}
module.exports.pop = () => {
this.refs.router.pop()
}
}
render() {
return <Router ref="router"/>
}
}
然后
//second.js
import { push, pop } from "first.js"
//class instantiation and other code
push("myRoute")
密码笔:https://codepen.io/Stefvw93/pen/bLyyNG?editors=0010
目的是避免使用 react-router 中的 withRouter 函数。因此,请从 react-router 的 browserRouter 组件的单个实例中公开推送/弹出历史功能。它的工作原理是创建对路由器实例的引用 (ref="router"),然后通过执行类似 module.exports.push=this.refs.router.push 之类的操作导出此实例
【问题讨论】:
-
这是一个问题吗?我说不出来。
标签: javascript reactjs react-router