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

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

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

app.js:

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

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

handleAppStateChange = (nextAppState) => {
  if (nextAppState === 'inactive') {
   AsyncStorage.removeItem('item');
  }    
}
react-native
1个回答
0
投票

我认为您的代码是正确的,只需要稍作调整:

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');
  }    
}

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