这个错误已经有一段时间了
Error: Objects are not valid as a React child (found: object with keys {id, msg}). If you meant to render a collection of children, use an array instead.
我已经找出罪魁祸首:
useEffect(() => {
db.collection("msgs")
.onSnapshot((snapshot) => {
setMessages(
snapshot.docs.map((doc) => ({
id: doc.id,
msg: doc.data(),
}))
);
});
}, []);
doc.data() 是一个对象而不是一个字符串,所以当我出去渲染它时:
{messages.map((msg, id) => {
return (
<Message key={id} msg={msg} />
);
})}
它抛出上面的错误。
我正在关注这个 github 存储库:https://github.com/leopaul29/facebook-messenger-clone/blob/master/src/App.js
有什么想法吗?