【发布时间】:2021-11-18 18:17:19
【问题描述】:
constructor(props) {
super(props);
this.state = {
message: []
};
}
async getData() {
await axios.get("https:...")
.then((response) => {
console.log((response));
console.log(typeof(response)); // object
const convertString = JSON.parse(response.data.body);
this.setState({message: convertString});
console.log(convertString));
})
}
componentDidMount() {
this.getData();
}
render() {
const obj = (this.state.message);
console.log(obj);
return(
<div>
{this.state.message}
</div>
)
我收到错误消息:“错误:对象作为 React 子项无效(找到:带有键 {Items, Count, ScannedCount} 的对象)。如果您要渲染一组子项,请改用数组。”
console.log(convertString) 给了我这个:
Items: Array(4)
0: {key1: "value", key2: "value"}
1: {key1: "value", key2: "value"}
2: {key1: "value", key2: "value"}
3: {key1: "value", key2: "value"}
如果我想以表格格式呈现数组,我应该如何通过 this.setState() 传递数据?
【问题讨论】:
标签: arrays reactjs object children