【问题标题】:How do I make these props usable to my other react files?如何使这些道具可用于我的其他反应文件?
【发布时间】: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


【解决方案1】:

问题在于 data.main 是一个对象(因为您引用了 data.main.temp)。您不能渲染对象。你可能打算写:

city: data.main.city

【讨论】:

  • @Nischal 欢迎您。但是为什么我的答案是-1? :-( –
猜你喜欢
  • 1970-01-01
  • 2019-08-04
  • 2017-11-20
  • 2015-04-23
  • 2020-04-04
  • 2021-04-19
  • 1970-01-01
  • 2019-10-17
  • 2018-09-15
相关资源
最近更新 更多