使用React Router dom从历史记录中删除状态

问题描述 投票:0回答:1

我正在使用react-router-dom我正在从屏幕A导航到屏幕B,并指定状态clearData。如下所示:

this.props.history.push({pathname: '/B', state: {clearData: true}})

现在,当我从屏幕B导航到屏幕C并返回到屏幕B时,我想清除屏幕B中的状态clearData。如何实现?

提前感谢:)

reactjs react-router react-router-dom browser-history
1个回答
1
投票

实现此用途

componentWillRecieveProps(props){
if(props.location.state !==this.props.location.state && 
   props.location.state.clearData){

   this.setState(initial_state_obj)

}

您也可以在componentDidMount事件上获取它!

© www.soinside.com 2019 - 2024. All rights reserved.