有很多問題可用,我嘗試了大約 12 種不同的方法,但都沒有奏效。最有效的一種是:
async function removeItemValue(key) {
try {
await AsyncStorage.removeItem(key);
return true;
}
catch (exception) {
return false;
}
}
然后我稱之為:
useEffect(() => {
removeItemValue('@cartInfo');
}, []);
我試過把它放在useEffect鉤子外面,但仍然沒有效果。我究竟做錯了什么?
更新:
也試過這個,但沒有奏效:
useEffect(() => {
removeItemValue('@cartInfo').then(() => { console.log('removed') })
}, []);
也試過
useEffect(() => {
AsyncStorage.removeItem('@cartInfo', () => {
AsyncStorage.getItem('@cartInfo').then((res) => {
console.log("RES IS: " res);
})
})
}, []);
仍然沒有運氣。我正在使用 @react-native-community/async-storage 的 v1.12.1
uj5u.com熱心網友回復:
正如我們在chat 中討論的那樣,AsyncStorage.removeItem實際上是有效的,問題是它setItem被呼叫得太頻繁了,因此該值在稍后被讀取之前被替換getItem。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357557.html
