【发布时间】:2017-12-19 13:44:50
【问题描述】:
import createBrowserHistory from 'history/
import appState from './AppState'
const customHistory = createBrowserHistory()
ReactDOM.render(
<Router>
<div>
<Route path="/submit" component={Submit} history={history}/>
</div>
</Router>,
document.getElementById('root')
);
注意: 如果我像其他属性一样添加 appState; Submit组件中没有appState等属性,
如果我用这个;
<Route path="/submit" render={routeProps=> <Submit appState={appState}/> history={history}/>
appState 可以正常通过,但是这次历史对象丢失了,这样做的正确方法是什么? appState 只是 mobx 类的实例。
已编辑:我找到了一个“奇怪”的解决方案。我把路由元素改成了这个;
<Route path="/submit" render={routeProps=> <Submit appState={appState}/> history={history}/>
然后我可以通过this._reactInternalInstance._context.router.history.push('/home') 访问相同的历史对象
在进行此更改之前,我可以将其用作this.props.history.push('/home')
如果我让它工作正常,但以这种方式获取这个历史对象很烦人
【问题讨论】:
-
您应该将
history对象传递给router,而不是route。如果我错了,请纠正我。 -
也许有共同点,但在这个示例中,我可以像“this.props.history.pushState('/previousPage')”一样使用它,所以我的问题不包括历史的机智使用跨度>