Reactjs Firestore:在promise()中调用collections.get()
是否有任何原因导致它无法检测新数组并触发新状态和渲染?从这里可以看到,它应该正在更新和重新渲染。您的内部查询Reactjs Firestore:在promise()中调用collections.get(),reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,是否有任何原因导致它无法检测新数组并触发新状态和渲染?从这里可以看到,它应该正在更新和重新渲染。您的内部查询doc.ref.collection('divisions')。get()不会强制当前组件重新渲染。简单地将元素推入数组并不会告诉组件需要渲染该数组中的内容 您必须使用一个状态钩子来告诉组件使用新数据再次渲染,类似于您已经在使用setEvent()和setStop()执行setDivisions(tempDivisions)内部doc.ref.collection('divisions')
doc.ref.collection('divisions')。get()
不会强制当前组件重新渲染。简单地将元素推入数组并不会告诉组件需要渲染该数组中的内容
您必须使用一个状态钩子来告诉组件使用新数据再次渲染,类似于您已经在使用
setEvent()
和setStop()
执行setDivisions(tempDivisions)
内部doc.ref.collection('divisions')。get()
,会再次渲染吗?我最初有这个,但它没有做任何事情。如果这是一个状态钩子方法,那么它应该会导致使用您提供的数据重新渲染。非常感谢。我以前有过这样的设置,我不知道为什么会抛出错误,我猜这种情况发生在你工作到很晚的时候,哈哈,你看看编辑2?我面临着无法重新渲染的状态。useState钩子正在使用,我认为这是正确的。如果你有新问题,你应该单独发布。堆栈溢出只允许每篇文章有一个问题。
useEffect(() => {
if (!stop) {
// get current user profile
db.collection('events').get(eventId).then((doc) => {
doc.forEach((doc) => {
if (doc.exists) {
let temp = doc.data()
let tempDivisions = []
temp["id"] = doc.ref.id
doc.ref.collection('divisions').get().then((docs) => {
docs.forEach(doc => {
let temp = doc.data()
temp["ref"] = doc.ref.path
tempDivisions.push(temp)
});
})
temp['divisions'] = tempDivisions
setEvent(temp)
setStop(true)
// setLoading(false);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
<Redirect to="/page-not-found" />
}
})
})
}
}, [stop, eventId]);
const [entries, setEntries] = useState([])
useEffect(() => {
let active = true
let temp = []
if (active) {
divisions.forEach((division) => {
let teams = []
let tempDivision = division
db.collection(`${division.ref}/teams`).get().then((docs) => {
docs.forEach((doc, index) => {
teams.push(doc.data())
})
tempDivision['teams'] = teams
})
setEntries(oldArray => [...oldArray, temp])
})
}
return () => {
active = false;
};
}, [divisions]);