组件DidMount上的状态清除



我在这里面临问题,我想要第二种意见。当我的应用程序的第一页被呈现时,出于安全原因,我想清除我的状态,所以在我的布局类组件中我写:

componentDidMount() {
this.props.clearState();
}

问题是,当我在应用程序的第二个页面上单击反斜杠时,我的状态将再次清除。我想在我的组件DidMount中设置一个条件,这样如果我来自应用程序的某个页面,就不会清除状态。这可能吗?你对如何做到这一点还有其他想法吗?非常感谢

如果你不使用Redux,如果clearState((被触发,你可以尝试sessionStorage临时存储。

//simple Example
componentDidMount() {
if (sessionStorage.getItem('clearState') === 0) {
this.props.clearState();
sessionStorage.setItem('clearState fired', 1)
}
} 

您应该尝试在componentWillUnmount((生命周期方法中清除组件的状态。这是进行清洁的合适地方。

最新更新