react-native - 删除 Firestore 中的文档不更新 onSnapshot
问题描述
我正在从 firestore 中获取 react native 的项目列表。
如果更新了项目文档,我的列表会通过 onSnapshot 按预期刷新。但是如果文档被删除,我的列表不会刷新。
有没有办法让我捕获已删除的文档?
this.unsubscribe = firebase.firestore().collection('bigItems').doc(id)
.collection('littleItems').onSnapshot(this.getItems)
getItems = (querySnapshot) => {
const items = [];
querySnapshot.forEach((doc) => {
firebase.firestore().collection('events').doc(doc.id).get()
.then(item => {
const { id } = item.data();
items.push({
id: item.id
});
this.setState({
items: items,
loading: false,
});
})
.catch(function (err) {
return err;
});
})
}
解决方案
尝试以下方法是否适合您:
const dataRef = firebase.firestore()
.collection('bigItems').doc(id)
.collection('littleItems').onSnapshot(res => {
// Listen to change type here
res.docChanges().forEach(change => {
const docName=change.doc.id;
const docData=change.doc.data();
// Refer to any field in the document like:
// E.g. : data.name, data.text etc.
// i.e. (whatever fields you used for the document)
if (change.type === "added") {
// Append to screen
}
if (change.type === "removed") {
// Now, Manually remove from UI
// Data from state before snapshot is still here,
// use some reference in data to remove from screen.
}
});
});
推荐阅读
- r - 无法弄清楚如何从虚拟变量标题中删除列名
- python - 如何在 Windows 10 机器上完全卸载 Python 3.7?
- java - android模拟器运行时显示空白屏幕
- python - 当我使用“~”指示用户主路径时,为什么 `open("~/something")` 不起作用?
- reactjs - React 将 MUI TextField 值设置为其初始状态
- angular - 如何在动态表中获取特定表数据(td)的宽度和高度
- jestjs - 如何在 Jest 27 中模拟 Prisma(通过代理)
- git - 试图克隆一个 git repo 它卡在克隆到
- flutter - 没有为类型“Rx”定义吸气剂“某个变量”
' 在使用 getx 控制器时 - linux - SELinux:允许未经许可的用户读取文件以生成能够读取此类文件的进程