【发布时间】:2019-04-17 01:22:25
【问题描述】:
我正在尝试渲染从 openweathermap 获取的数据,但我不断收到以下错误:
对象作为 React 子对象无效(发现:对象带有键 {coord, weather, base, main, visibility, wind, clouds, dt, sys, id, name, cod})。如果您打算渲染一组子项,请改用数组。
import React from 'react';
import axios from 'axios';
class PersonList extends React.Component {
state = {
persons: []
};
componentDidMount() {
axios
.get(
`https://api.openweathermap.org/data/2.5/weather?q=Toronto&appid=abdeb978cd944502164274a08638f7ac`
)
.then(res => {
const persons = res.data;
this.setState({ persons });
});
}
render() {
return <div>{this.state.persons}</div>;
}
}
export default PersonList;
【问题讨论】:
标签: reactjs