本文介绍了检测反应路由器中的先前路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用反应路由器.我想检测我来自哪里的上一页(在同一个应用程序中).我的上下文中有路由器.但是,我在路由器对象上看不到任何属性,例如以前的路径"或历史记录.我该怎么做?
I am using react router. I want to detect the previous page (within the same app) from where I am coming from. I have the router in my context. But, I don't see any properties like "previous path" or history on the router object. How do I do it?
推荐答案您可以在 componentWillReceiveProps 生命周期方法中保存以前的路径.该逻辑非常接近 react-router 文档的 noreferrer">疑难解答部分.
You can save previous path in a componentWillReceiveProps lifecycle method. The logic is very close to the example provided in troubleshooting section of react-router docs.
<Route component={App}> {/* ... other routes */} </Route> const App = React.createClass({ getInitialState() { return { prevPath: '' } }, componentWillReceiveProps(nextProps) { if (nextProps.location !== this.props.location) { this.setState({ prevPath: this.props.location }) } } })最近,从状态访问它.
更多推荐
检测反应路由器中的先前路径?
发布评论