我正在使用 React router v4,在重新加载页面时遇到一些问题(不是 window.location.reload)。我最好给出一个真实的用例来解释这个问题,我们使用社交网络应用程序作为示例:
- 用户 A 评论了用户 B 的帖子,用户 B 页面中会出现一条通知。
- 用户 B 单击了通知,我们执行了
this.props.history.push('/job/' + id')
,它成功了,因此用户 B 去了job/123
page.
- 用户A再次评论,用户B页面出现新通知,而用户B仍然停留在
job/123
页面上,他点击了通知链接并触发了this.props.history.push('/job' + id')
。但他不会看到页面重新呈现,他没有看到最新的评论,因为该页面没有执行任何操作。
在许多情况下,这似乎是一种常见的情况。可以使用许多不同的方法来解决这个问题。检查这个堆栈溢出问题 https://stackoverflow.com/questions/38839510/forcing-a-react-router-link-to-load-a-page-even-if-were-already-on-that-page。有一些很好的答案和发现。亲自这种方法 https://stackoverflow.com/a/53837248/1453339对我来说更有意义。
location.key
每当用户尝试在页面之间导航时(即使在同一页面内),每次都会发生变化route
。要在您的代码块下面测试这个地方/jod/:id
成分:
componentDidUpdate (prevProps) {
if (prevProps.location.key !== this.props.location.key) {
console.log("... prevProps.key", prevProps.location.key)
console.log("... this.props.key", this.props.location.key)
}
}
我也遇到过同样的情况。更新状态为componentDidUpdate
。之后就按预期工作了。单击同一路线中的项目会更新状态并显示正确的信息。
我假设(不确定你如何传递/更新评论/job/:id
)如果你在你的/job/:id
组件应该工作:
componentDidUpdate (prevProps) {
if (prevProps.location.key !== this.props.location.key) {
this.setState({
comments: (((this.props || {}).location || {}).comments || {})
})
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)