【发布时间】:2019-11-19 04:43:56
【问题描述】:
我正在编写一个 react-native 应用程序,它需要在关闭或后台之前存储一个字符串。当应用程序启动时,它需要再次检索字符串。 (我正在使用 expo 进行开发)
我尝试将数据存储在componentWillUnmount(),但是当我关闭应用程序时该函数没有被调用。
这是存储数据的功能。
_storeData = async () => {
try {
await AsyncStorage.setItem('data', 'savedData2');
console.log("done storing");
} catch (error) {
console.log(error)
}};
我打电话给storeData()如下:
componentWillUnmount() {
console.log("unmount")
this._storeData();
}
首先,我认为它不存储数据,因为我在componentWillUnmount() 中调用了一个异步函数,该函数可能在完成之前被取消,但是我也没有收到unmount 日志。
【问题讨论】:
-
您是否设法使其工作(关闭应用程序时存储数据)?怎么样?
标签: react-native asyncstorage react-component