我正在尝试将用户从页面重定向到同一页面,并在用户登录后与查询参数buy=true
。,我试图使用react-router-redux
中的推动功能来推动状态。这无效。replace
也没有功能。新URL已更新,但我的组件没有再次渲染。正确的方法是什么?
这就是我尝试的
dispatch(push(/home?buy=true))
dispatch(replace(/home?buy=true))
browserHistory.push(/home?buy=true)
我认为您可以尝试这样的方法:
-
创建一个本地状态,以使您想要重新渲染的元素
-
内部 componentWillReceiveProps 或 componentdidupdate ,您应该触发a setState 更改当前状态
类似的东西:
constructor() {
this.state = {
hasRefreshed: false
}
componentWillReceiveProps(nextProps) {
const paramsHaveChanged = nextProps.location.query !== this.props.location.query
if (paramsHaveChanged) {
this.setState({ hasRefreshed: true })
}
if (this.state.hasRefreshed) {
this.setState({ hasFrefreshed: false })
}
}
render() {
if (hasRefreshed) return null
return() {
//other elements
}
}