当用户终止应用程序时,我想从API更改状态数据。
我尝试使用componentWillUnmount在应用程序关闭时更改数据,我也使用AppState
_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!')
}
this.setState({ appState: nextAppState })
}
我希望当应用程序被用户杀死时,它可以自动更改状态。
尝试这个 :
componentDidMount() {
AppState.addEventListener('change',
this.handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
}
handleAppStateChange = (nextAppState) => {
if (nextAppState === 'inactive') {
console.log('the app is closed');
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句