從我的后端,我收到表單上的資料(或物件){val1: Array(100), val2: Array(100)}。
如果我console.log得到,那么,控制臺中包含上述內容的物件。但是,如果我嘗試將其保存為useState:
const [data, setData] = useState()
并嘗試在return我的 React 組件中顯示/輸出它,例如:
return (
<div>{data}</div>
)
我得到錯誤:
Uncaught Error: Objects are not valid as a React child (found: object with keys {val1, val2}). If you meant to render a collection of children, use an array instead.
我希望我可以做類似的事情:{data.val1}然后我從中得到資料等等。但是現在我可能需要映射所有內容,或者我在這里做錯了什么?
uj5u.com熱心網友回復:
您不能在 jsx 中使用整個資料物件,正如您所說,您需要使用物件鍵來訪問像data.val1. 如果資料物件很大,您可以使用 Object.keys(data) 并將鍵映射到物件上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415565.html
標籤:
上一篇:反應從孩子到父母的更新陣列道具
下一篇:Axios多個then的
