gpt4 book ai didi

javascript - 如何将多个项目推送到一个数组中并加载到 native react 中的异步存储中?

转载 作者:行者123 更新时间:2023-11-30 19:00:38 25 4
gpt4 key购买 nike

我正在尝试将数据推送到 native react 中的本地存储中,在这种情况下推送多个元素。我正在尝试使用此处指出的文档:

How do I set multiple values within Asyncstorage

我将如何正确地执行此操作?下面是一些代码:

我现在在做什么

const STORAGE_KEY = '@save_enableauto';
const DBLTIME_KEY = '@save_dbltime';

state={
times: Times,
messageTimes: {
dblTime: '12:00 pm',
genTime: '12:00 pm'
}
enableAuto:false
}


//retrieves automatic messaging status
_retrieveData = async () => {
try {

//pull data from local storage
const enableAuto = await AsyncStorage.getItem(STORAGE_KEY);
const dblTime = await AsyncStorage.getItem(DBLTIME_KEY);

console.log('auto messages set: ',enableAuto);
console.log('time data is:', dblTime);

//reset state for time if it exists in local storage
if(dblTime !==null) {
this.setState(prevState => ({
messageTimes: { // object that we want to update
...prevState.messageTimes, // keep all other key-value pairs
dblTime: dblTime // update the value of specific key
}
}))
}

//reset state for notifications if exists in local storage
if (enableAuto !== null) {
// We have data!!
console.log('receiving from local storage: ',enableAuto);
this.setState({ enableAuto:eval(enableAuto) });
}
} catch (error) {
alert('failed to load previous settings.')
// Error retrieving data
}
};

//trying to set it up with one call
_retrieveDataGroup = async () => {

const items = JSON.stringify([['k1', STORAGE_KEY], ['k2', DBLTIME_KEY]]);

try {
const localData = AsyncStorage.multiGet(items, () => {
//to do something
});

console.log('Group fetch: ',localData);

} catch (error) {
alert('failed to load previous settings.')
// Error retrieving data
}
};

现在,当控制台日志组提取时我收到的是一个 promise :

Group fetch:  Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}

最佳答案

multiGet 是一个 Promise。在调用之前添加 await

const localData = await AsyncStorage.multiGet(items, () => {
//to do something
});

关于javascript - 如何将多个项目推送到一个数组中并加载到 native react 中的异步存储中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59546874/

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