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

问题描述:

我正在使用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 
    } 
} 

任何帮助,将不胜感激。

+0

据我所知'someValue'不会在位置状态刷新页面后。所以你不需要手动删除它。 –

我认为你可以使用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: {} 
    }); 
    } 
} 
+0

谢谢。这有帮助。 – duwalanise

+0

不客气:)我很高兴我能帮你。 –