javascript - 无法通过映射对象 ReactJS 来渲染组件
问题描述
我正在使用 ethereum blochchain 和 ReactJS 开发一个 webapp。在一个页面中,在 componentDidMount() 内部,我从区块链收集数据并将其添加到名为 requests 的数组中
typeof(requests) : object
console.log(requests) :(2) [Result, Result]
0: Result
0: "Title"
1: "100000000000000"
2: "0xbA439F3C91bF0732e5546721A09be207f69555ca"
3: false
4: "0"
approvalCount: "0"
complete: false
description: "Title"
recipient: "0xbA439F3C91bF0732e5546721A09be207f69555ca"
value: "100000000000000"
__proto__: Object
1: Result
0: "New Request"
1: "4000000000000000000"
2: "0x8104Ce1f3d731A5C39501fddDdc14E2673555555"
3: false
4: "0"
approvalCount: "0"
complete: false
description: "New Request"
recipient: "0x8104Ce1f3d731A5C39501fddDdc14E2673555555"
value: "4000000000000000000"
__proto__: Object
length: 2
__proto__: Array(0)
我尝试使用以下代码根据请求的元素呈现组件:
<body>
{this.state.requests.map((request, index) => {
return (
<RequestRow
key={index}
id={index}
address={this.state.address}
request={request}
approversCount={this.state.approversCount}
/>
);
})}
</body>
但是出现这个错误
Error: Objects are not valid as a React child (found: object with keys {id, address, request, approversCount}). If you meant to render a collection of children, use an array instead.
如何解决这个问题?
RequestRow 不完整
它被实现为
import React from "react";
const requestRow = (props) => <div>{props}</div>;
export default requestRow;
解决方案
您不需要为空数组条件编写代码。你只需要写,
<body> {
this.state.requests.map((request, index) => {
return ( <RequestRow key = {
index
}
id = {
index
}
address = {
this.state.address
}
request = {
request
}
approversCount = {
this.state.approversCount
}
/>
);
});
}</body>
推荐阅读
- google-apps-script - Google Forms/Sheets - 如何强制使用有界脚本复制的表单文件为其 onFormSubmit 事件添加触发器?
- redis - Redis:如果我对 HA 不感兴趣,只使用分片有意义吗?
- django - 仅显示登录用户的项目
- react-native - 呈现或评估动态标签字符串
- swagger - jhipster 与 webflux - Swagger 不工作
- java - 检查数组列表子字符串的方法和循环似乎在 Java 中没有返回正确的值
- python - 将多个 seaborn 绘图保存到一个 pdf 文件中
- javascript - 如何将 json 文件从我的计算机导入到 jsfiddle
- python - Python pandas to_sql 用 NA 填充所有行
- arrays - 如何在 Laravel 中打印带方括号而不是大括号的数组?