【发布时间】:2021-10-31 03:49:39
【问题描述】:
所以我的 firebase 有 30k++ 大数据。它的坏处是,每次我在我的 React 页面上单击返回按钮时,它都会再次获取 30k++ 数据,这会伤害钱包。
有什么办法可以解决吗?
这是我在主页中使用的useEffect
useEffect(() => {
// first 5 posts
Form.allPosts()
.then((res) => {
setSalesLastMonth(res.posts);
setLastKey(res.lastKey);
})
.catch((err) => {
console.log(err);
});
Form.postsToday()
.then((res) => {
setSales(res.posts)
}).catch((err) => {
console.log(err)
})
}, []);
这是 firebase 函数的示例:
postsToday: async function () {
try {
const yesterday = new Date(new Date().setHours(0, 0, 0, 0));
let today = new Date();
const data = await firestore
.collectionGroup('form')
.orderBy("Created At", "desc")
.where('Created At', '>=', yesterday)
.where('Created At', '<=', today)
.get()
let posts = [];
let lastKey = "";
data.forEach((doc) => {
posts.push({
id: doc.id,
etc etc
});
lastKey = doc.data()['Created At']
});
return { posts, lastKey };
} catch (e) {
console.log(e);
}
},
有人可以帮我吗?
【问题讨论】:
标签: reactjs firebase google-cloud-firestore