为什么这个 getPosts 函数导致我的 firestore 数据库中的读取大量使用?
Why is this getPosts function causing major usage of reads in my firestore database?
function Home({ isAuth }) {
const [postLists, setPostList] = useState([]);
const postsCollectionRef = collection(db, "posts")
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) =>
({ ...doc.data(), id: doc.id })));
};
getPosts();
});
这是控制台日志的屏幕截图。 Db 说我有超过 56k 的阅读。
您的 useEffect 挂钩似乎缺少依赖项数组。尝试将其替换为以下内容,看看是否能解决您的问题!
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) =>
({ ...doc.data(), id: doc.id })));
};
getPosts();
});
}, [])
function Home({ isAuth }) {
const [postLists, setPostList] = useState([]);
const postsCollectionRef = collection(db, "posts")
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) =>
({ ...doc.data(), id: doc.id })));
};
getPosts();
});
这是控制台日志的屏幕截图。 Db 说我有超过 56k 的阅读。
您的 useEffect 挂钩似乎缺少依赖项数组。尝试将其替换为以下内容,看看是否能解决您的问题!
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) =>
({ ...doc.data(), id: doc.id })));
};
getPosts();
});
}, [])