检测反应路由器中的先前路径? [英] Detect previous path in react router?

查看:31
本文介绍了检测反应路由器中的先前路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 生命周期方法中保存以前的路径.该逻辑非常接近 疑难解答部分.

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

最近,从状态访问它.

这篇关于检测反应路由器中的先前路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆