首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从React中的api渲染?

如何从React中的api渲染?
EN

Stack Overflow用户
提问于 2019-08-04 14:35:17
回答 4查看 70关注 0票数 0

我已经使用NASA的api成功地更改了状态。现在,我想显示来自api的标题、图像和解释。我是个初学者,所以对我不要太苛刻!

我已经尝试过搜索和尝试不同的代码,但都没有成功。不知道这里有没有人能指出我做错了什么。

代码语言:javascript
运行
复制
this.state = {
  picture: "",
  date: ""
};
componentDidMount(){
  fetch(`https://api.nasa.gov/planetary/apod?api_key=${API_KEY}`)
    .then(response => response.json())
    .then(json => this.setState({ picture: json }));
};
render(){
  return (
    <div className="container">
      <h1>NASA Picture of the Day</h1>
      <form onSubmit={this.handleSubmit}>
        (YYYY-MM-DD):
        <input
          type="text"
          id="date"
          placeholder="input date"
          value={this.state.date}
          onChange={this.handleChange}
        />
        <button type="submit" disabled={!this.state.date}>
          Submit
        </button>
      </form>
    </div>
  );
};
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-08-04 14:57:27

目前来自NASA API的示例响应如下:( NOt确定将来是否会发生变化)

代码语言:javascript
运行
复制
{
"date": "2019-08-04",
"explanation": "Twenty-one years ago results were first presented indicating that most of the energy in our universe is not in stars or galaxies but is tied to space itself.  In the language of cosmologists, a large cosmological constant -- dark energy -- was directly implied by new distant supernova observations.  Suggestions of a cosmological constant were not new -- they have existed since the advent of modern relativistic cosmology. Such claims were not usually popular with astronomers, though, because dark energy was so unlike known universe components, because dark energy's abundance appeared limited by other observations, and because less-strange cosmologies without a signficant amount of dark energy had previously done well in explaining the data. What was exceptional here was the seemingly direct and reliable method of the observations and the good reputations of the scientists conducting the investigations. Over the two decades, independent teams of astronomers have continued to accumulate data that appears to confirm the existence of dark energy and the unsettling result of a presently accelerating universe. In 2011, the team leaders were awarded the Nobel Prize in Physics for their work.  The featured picture of a supernova that occurred in 1994 on the outskirts of a spiral galaxy was taken by one of these collaborations.    News: APOD is now available via Facebook in Hindi.",
"hdurl": "https://apod.nasa.gov/apod/image/1908/SN1994D_Hubble_2608.jpg",
"media_type": "image",
"service_version": "v1",
"title": "Rumors of a Dark Universe",
"url": "https://apod.nasa.gov/apod/image/1908/SN1994D_Hubble_960.jpg"
}

在同一组件中显示来自NASA的一些信息(假设您希望在单击提交按钮之前显示详细信息)

代码语言:javascript
运行
复制
let picture = this.state.picture;
 return (
      <div className="container">
        <h1>NASA Picture of the Day</h1>
        <h2>{picture.title}</h2>
        <img src={picture.url} alt={picture.title}></img>
        <p>{picture.explanation}</p>
         ___________ YOUR FORM INPUT CONTROLS _____________
      </div>
    );
票数 1
EN

Stack Overflow用户

发布于 2019-08-04 14:44:51

您没有使用this.state.picture anywhere。只需使用现有的任何数据来渲染图像即可。

我不确定数据是什么格式,但假设API返回JSON,如下所示:

代码语言:javascript
运行
复制
{ "url": "http://nasa.gov/path/to/image.png" }

那么你只需要在某个地方找到它:

代码语言:javascript
运行
复制
{ this.state.picture && <img src={this.state.picture.url} /> }
票数 1
EN

Stack Overflow用户

发布于 2019-08-04 14:43:50

当您在图片中存储数据时,请在render中写入以下内容

代码语言:javascript
运行
复制
return (<ul>this.pictureToDisplay()</ul>)

pictureToDisplay定义:- (假设这是一个图片数组)

代码语言:javascript
运行
复制
return (this.state.picture.map(item=><li>item</li>))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57344380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档