【发布时间】:2019-10-05 19:31:40
【问题描述】:
我有这个数组:
{
"firstname": "Jessica",
"lastname": "Wood",
"favorite": {
"movie": Flint Town,
"drink": Sprite
}
}
我正在尝试喝酒,但我发现 Objects are not valid as a React child found: object with keys.
我该如何解决这个问题?
我的反应组件:
import React, { Component } from 'react';
import axios from 'axios';
class Home extends Component {
state = {
info: []
}
componentDidMount() {
axios.get('http://localhost:9000/api')
.then(res => {
const info = res.data;
this.setState({ info });
})
}
render() {
return (
<ul>
<li>{this.state.info.favorite.drink}</li>
</ul>
)
}
}
export default Home;
【问题讨论】:
-
Sprite 是字符串吗?您需要将“最喜欢”对象中的值转换为字符串或数字,否则您将无法呈现它们。
-
嗨@slower,是
info和Array还是Object -
@ramirozap 它是数组,当我这样做时 this.state.info.firstname 它工作正常。
-
@slower。 Flint Town 和 Sprite 似乎不是字符串。您需要将它们转换为字符串才能正常工作。
标签: javascript arrays node.js json reactjs