小编典典

导航到某些路径后清除位置状态

reactjs

我正在使用react-router browserHistory导航到路径。

browserHistory.push({
  pathname: '/mycomponent',
  state: { someValue: 'value'},
});

因此,这将导航到mycomponent。我一接触到我的组件,便想清除钥匙someValue。这样,当我刷新页面时,它将不包含该值。

export class myComponent extends component {
  componentWillMount() {
    const value = this.props.location.state.someValue;
    // clear state.someValue from history
  }
}

任何帮助,将不胜感激。


阅读 289

收藏
2020-07-22

共1个答案

小编典典

我认为您可以使用browserHistory replace方法将历史状态替换为someValue未定义的新状态:

export class myComponent extends component {
    componentWillMount() {
        const value = this.props.location.state.someValue;
       // clear state.someValue from history
        browserHistory.replace({
           pathname: '/mycomponent',
           state: {}
       });
    }
}
2020-07-22