检测反应路由器中的先前路径?

编程入门 行业动态 更新时间:2024-10-26 19:33:55
本文介绍了检测反应路由器中的先前路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用反应路由器.我想检测我来自哪里的上一页(在同一个应用程序中).我的上下文中有路由器.但是,我在路由器对象上看不到任何属性,例如以前的路径"或历史记录.我该怎么做?

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 }) } } })

最近,从状态访问它.

更多推荐

检测反应路由器中的先前路径?

本文发布于:2023-11-25 16:35:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   先前   路径   器中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!