本文介绍了导航到某个路径后清除位置状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using react-router browserHistory to navigate to path.

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

所以这将导航到 mycomponent.一旦我到达我的组件,我想清除键 someValue.这样当我刷新页面时,它就不会包含该值.

So this will navigate to mycomponent. As soon as i reach to mycomponent i want to clear key someValue. so that when i refresh the page it won't contain that value.

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

任何帮助将不胜感激.

推荐答案

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

I think that you can use browserHistory replace method to replace history state with new one without someValue defined:

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

这篇关于导航到某个路径后清除位置状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 05:49