【发布时间】:2022-01-14 17:52:33
【问题描述】:
当我完成从数据库中获取数据并使用状态 setValue 并传递数组时,渲染函数不会再次渲染。
我设置数组的代码
function getNotifiche(){
onSnapshot(collection(db, "Users",auth.currentUser.uid, "Notifiche"), async (snapNotifiche) => {
const notifichee = [];
snapNotifiche.forEach(async notificaDoc => {
const docRef = doc(db,"Users",notificaDoc.data().Invitato);
const userData = await getDoc(docRef);
notifichee.push({
"data":notificaDoc.data(),
"invitante":userData.data()
})
});
setListaNotifiche(notifichee);
});
这是在返回函数中:
{
(listaNotifiche.map((notifica,index)=>{
return(<div key={index}>
<label>Invitato da {notifica.invitante.Username} in data{new Date(notifica.data.DataInvito.seconds * 1000).toString()}</label>
<button>Partecipa</button>
<button>Elimina</button>
</div>);
}))
}
所以我也尝试使用 setListaNotifiche([...notifichee]) 但我收到错误:TypeError: Cannot read properties of undefined (reading 'map')
【问题讨论】:
-
你做了哪些调试来确认状态没有被更新?并且该错误没有发生在状态完全更新?
getNotifiche什么时候被调用过?在那之前的状态值是多少?初始状态是什么?请提供minimal reproducible example 来证明问题。 -
@David 我已经控制台记录了数组,当它已满时,我没有看到任何显示。在代码的第一个版本中,我只推送了数组中的 notificheDoc.data(),现在它可以工作了,因为数组是由渲染没有更新的对象组成的
标签: javascript reactjs