【发布时间】:2018-03-11 19:41:59
【问题描述】:
this.setState({
temperature: data.main.temp,
city: data.main,
country: data.sys.country,
humidity: data.main.humidity,
description: data.weather[0].description,
error: ""
});
当我尝试从另一个文件调用这些道具时,它会显示此错误
对象作为 React 子对象无效(找到:带有键 {temp、pressure、湿度、temp_min、temp_max} 的对象)。如果您打算渲染一组子项,请改用数组。
这就是我一直试图调用道具的方式。
<Weather
temperature={this.state.temperature}
city={this.state.city}
country={this.state.country}
humidity={this.state.humidity}
description={this.state.description}
error={this.state.error}
/>
【问题讨论】:
-
你的初始状态在哪里?像 state = { temperature: 0 } 之类的东西。
-
state = { 温度:未定义,城市:未定义,国家:未定义,湿度:未定义,描述:未定义,错误:未定义};
-
我说的是道具。
-
您访问的是 city: data.main 而不是 city: data.main.city 或类似的东西吗?
标签: reactjs