[关闭本地应用程序后如何从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中看到任何内容
您也可以使用相同的componentWillUnmount
来做到这一点
componentWillUnmount() {
AsyncStorage.removeItem('item');
AppState.removeEventListener('change', this.handleAppStateChange);
}
假设此文件是js根文件,并且仅在应用关闭时才会卸载
此用例不需要AppState
。您可以在componentWillUnmount
中删除AsyncStorage项。
componentWillUnmount() {
AsyncStorage.removeItem('item');
}