2017-04-19 69 views
0

我正在使用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'不會在位置狀態刷新頁面後。所以你不需要手動刪除它。 –

回答

4

我認爲你可以使用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

不客氣:)我很高興我能幫你。 –