AsyncStorage未在组件加载时递增值



我正在使用AsyncStorage和React Native。我有一个在useEffect中调用的函数,它可以工作,但我的AsyncStorage值没有得到更新。上面写着"1"。我将AsyncStorage作为一个阵列,我也尝试过传统的方法,并且该值不会在加载时递增。我已经在IOS模拟器和IOS设备上进行了测试,但没有运气。下面是在useEffect(((,[](中运行的函数

const addFunction = async () => {
var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
try {
if(storage_array) {
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
flow_complete++;
storage_array.push(flow_complete);
//storage_array.push(flow_test);
console.log('THIS IS flow_complete', flow_complete);
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
} else {
flow_complete = 0;
console.log('Storage array is empty')
}
} catch (error) {
console.log(error);
}
}

您需要设置项来更新存储。

const addFunction = async () => {
var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
try {
if(storage_array) {
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
flow_complete++;
storage_array.push(flow_complete);
//storage_array.push(flow_test);
console.log('THIS IS flow_complete', flow_complete);
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array));
} else {
flow_complete = 0;
console.log('Storage array is empty')
}
} catch (error) {
console.log(error);
}
}

最新更新