gpt4 book ai didi

javascript - react native 。应用程序的关闭事件

转载 作者:行者123 更新时间:2023-11-30 08:22:33 25 4
gpt4 key购买 nike

在 react native 上为 ios 开发的应用程序的页面之一有一个开关组件。与它们进行交互时,开关的状态存储在 Redux 存储中,但在离开页面或离开应用程序时,需要将状态数据发送到服务器。离开页面实现如下:

props.navigation.addListener('willBlur', async() => {
props.pushDataOnServer(data, token);
});

我们为 willBlur 事件添加了一个导航处理程序 (StackNavigator),因此我们可以捕获到另一个页面的转换,但是如果直接从开关的编辑页面关闭应用程序呢?这是否有任何事件(例如 willExitwillClose)?或者如果你知道更有效的方法,请告诉我们

最佳答案

在您的根组件中,您可以观察 AppState 的变化。

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

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

handleAppStateChange = (nextAppState) => {
if (nextAppState === 'inactive') {
console.log('the app is closed');
}
}

关于javascript - react native 。应用程序的关闭事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51182419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com