我目前正在传递路由更改的状态,如下所示:
View Details
我的逻辑是,如果"location.state.transaction“存在,则不获取新数据,否则,获取数据。
现在的缺陷是当页面重新加载时。如果用户重新加载页面,应用程序需要获取新数据。我以为如果重新加载,"location.state“就会被清除,但很明显,这个状态是保存在sessionStorage中的。
我该如何解决这个问题呢?我可以每次都获取新数据,但它不应该在单击“View Details”链接时获取数据。
发布于 2018-07-12 03:01:01
我也遇到了这个问题,我最终做的是从react路由器检索浏览器历史记录并清除特定的location.state属性。所以在你的情况下,它将是transaction
。我是在
componentDidMount
因此,在您第一次访问该页面后,该属性将不再存在,
import createHistory from 'history/createBrowserHistory'
...
componentDidMount(){
const history = createHistory();
if (history.location.state && history.location.state.transaction) {
let state = { ...history.location.state };
delete state.transaction;
history.replace({ ...history.location, state });
}
}
发布于 2019-06-02 20:31:18
有一种更好的方法,不用使用三方库。
我们可以使用history.replace()
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/history.md
componentDidMount(){
const {location,history} = this.props;
//use the state via location.state
//and replace the state via
history.replace()
}
发布于 2021-02-25 06:41:03
如果你正在使用react钩子,你可以使用window.history
直接清除状态,而不触发重现。这比使用useHistory
胡克的
replace
方法,这将导致组件重新呈现。
window.history.replaceState({}, document.title)
https://stackoverflow.com/questions/40099431
复制相似问题