[英]Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead
[英]Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead
我收到了這個錯誤,我不太明白。 這是我的代碼。
export default function Main() {
const [dates, setDates] = useState([]);
function getData() {
var ref = db.collection("posts");
var q = ref.get().then((ss) => {
ss.forEach((doc) => {
setDates([...dates, doc.data().born]);
});
});
}
return (
<div>
<h1>Working</h1>
<button onClick={getData}>Click me!!!</button>
{dates.map((value, index) => {
return <p key={index}> {{ value }} </p>;
})}
</div>
);
}
我不明白為什么它將值讀取為 object,因為當我添加到數組“日期”時,我添加了一個數字。 所以它應該只是一個數字數組。 我真的很困惑,我與傳播運算符並不是那么相似。 例如,如果我要console.log(doc.data().born)
它會給出一個像2001
這樣的數字。 任何幫助深表感謝
您通過附加{ }
將您的value
原語顯式轉換為 object ({value:'blabla'})
。 該插值用於Angular (and more) 。 在React中,基於 JS 的代碼只需要一個{ }
包裝器,因此{ value }
就是您所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.