【发布时间】:2019-06-10 08:36:42
【问题描述】:
我有一个旨在显示来自 API 的数据的组件:
class Item extends Component {
constructor(props) {
super(props);
this.state = {
output: []
}
}
componentDidMount() {
fetch('http://localhost:3005/products/157963')
.then(response => response.json())
.then(data => this.setState({ output: data }));
}
render() {
console.log(this.state.output);
return (
<ItemPanel>
<ItemBox>
<BoxTitle>{this.state.output}</BoxTitle>
</ItemPanel>
);
}
export default Item;
console.log(this.state.output) 返回正确的数据,而我的组件不会呈现,它会抛出这个错误:
Objects are not valid as a React child (found: object with keys {id, general, brand, images}). If you meant to render a collection of children, use an array instead.
我猜来自 api 的数据是一个对象。我已经尝试过使用 JSON.parse 或 toString() :/
【问题讨论】:
-
你能分享一下
this.state.output它的控制台吗?您到底想在id, general, brand, images中的<BoxTitle>中显示什么? -
让我们尝试在这里展示品牌,console.log 在上面
标签: javascript reactjs api