我正在尝试从 expo-async-storage 读取数据,将数据设置为变量并检查它是否包含 searchString。
首先,我获取当前的 MovieID
useEffect(() => {
const fetchMovieID = async () => {
try {
const fetchedID = await item.id;
setMovieID(fetchedID);
} catch (error) {
console.error("Error fetching movie ID:", error);
}
};
fetchMovieID();
}, [item]);
然后我创建第二个 useEffect
useEffect(() => {
setLoading(true);
getMovieDetails(item.id);
getMovieCredits(item.id);
getSimilarMovies(item.id);
if (movieID !== "") {
getData("Movies");
}
}, [item, movieID]);
const getData = async (key) => {
try {
const value = await AsyncStorage.getItem(key);
if (value !== null) {
console.log("Readed value:", value);
const isAlreadyAdded = favouriteMovies.includes(movieID);
console.log("Finding status:", isAlreadyAdded);
if (!isAlreadyAdded) {
setFavouriteMovies((prevMovies) => [...prevMovies, movieID]);
} else {
console.log("Cant find");
console.log("Movie ID", movieID);
}
}
} catch (e) {
console.log("Error reading data:", e);
}
};
问题在于,即使 isAlreadyAdded 变量正确设置为 false,'if' 中的代码也不会执行。
来自控制台的日志:
LOG Readed value: 1139829 LOG Finding status: false
我尝试创建另一个异步函数并等待分配布尔变量。 当然,无需分配布尔变量。
我自己想出来的。 首先,我创建了一个包含所有异步存储常量函数的新文件:
import AsyncStorage from "@react-native-async-storage/async-storage";
const getAllKeys = async () => {
let keys = []
try {
keys = await AsyncStorage.getAllKeys()
} catch(e) {
console.log('Get all keys error: ', e)
}
return keys
}
const setObjectItem = async (key, value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem(key, jsonValue)
} catch(e) {
cconsole.log('Error wthile saving object: ', e)
}
console.log('Properly saved: ', value)
}
const getObjectItem = async (key) => {
try {
const jsonValue = await AsyncStorage.getItem(key)
return jsonValue != null ? JSON.parse(jsonValue) : null
} catch(e) {
console.log('Error while getting object: ', e)
}
console.log('Object successfully readed')
}
export default {
getAllKeys,
setObjectItem,
getObjectItem,
}
然后,在另一个屏幕上,我从 API 获取我的 movieID:
import AsyncStorageOperations from "../src/components/AsyncStorageOperations";
const [movieID, setMovieID] = useState("");
useEffect(() => {
const fetchMovieID = async () => {
try {
const fetchedID = await item.id;
setMovieID(fetchedID);
} catch (error) {
console.error("Error fetching movie ID:", error);
}
};
fetchMovieID();
}, [item]);
在另一个 useEffect 中,我正在执行 checkMoviesStorage() 函数:
useEffect(() => {
(..)
checkMoviesStorage();
(..)
}, [item, movieID]);
const checkMoviesStorage = async () => {
if (movieID !== "") {
if ((await AsyncStorageOperations.getAllKeys().length) !== 0) {
console.log("KeysArray is not empty");
emptyKeysArray = false;
movieKeys = await AsyncStorageOperations.getStringValue("movie");
console.log(movieKeys, movieID);
if (movieKeys.includes(movieID)) {
console.log("Incredible, found!");
} else {
console.log("Nothing");
}
} else {
console.log("KeysArray is empty");
emptyKeysArray = true;
console.log(emptyKeysArray);
}
}
};
在按“添加到收藏夹”后调用的函数中,我更改图标的颜色,设置小吃栏并在满足条件时添加到收藏夹:
function manageFavourites() {
setSnackbarVisile(!snackbarVisible);
toggleFavourite(!isFavourite);
if (isFavourite) {
console.log("Deleting from favourites");
setSnackbarText("Deleting from favourites");
} else {
console.log("Adding to favourites");
setSnackbarText("Adding to favourites");
AsyncStorageOperations.setObjectItem("movie", movieID);
}
}