如何关闭本机应用程序之前从AsyncStorage中删除项目

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

[关闭本地应用程序后如何从AsyncStorage中删除项目?

app.js:

state = {
    appState: AppState.currentState
}

componentDidMount() {
  AppState.addEventListener('change', this.handleAppStateChange);
}

componentWillUnmount() {
  AppState.removeEventListener('change', this.handleAppStateChange);
}

handleAppStateChange = (nextAppState) => {
  if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
   AsyncStorage.removeItem('item');
 console.log("App has come to the foreground!");
  }
this.setState({ appState: nextAppState });    
}

我从没在console.log中看到任何内容

react-native
2个回答
0
投票

您也可以使用相同的componentWillUnmount来做到这一点

componentWillUnmount() {
          AsyncStorage.removeItem('item');
          AppState.removeEventListener('change', this.handleAppStateChange);
 }

假设此文件是js根文件,并且仅在应用关闭时才会卸载


0
投票

此用例不需要AppState。您可以在componentWillUnmount中删除AsyncStorage项。

componentWillUnmount() {
    AsyncStorage.removeItem('item');
}
© www.soinside.com 2019 - 2024. All rights reserved.