【问题标题】:How to fetch images in json api in react?如何在反应中获取json api中的图像?
【发布时间】:2019-06-20 19:53:25
【问题描述】:

所以我的问题是不言自明的,我正在尝试从此 api https://www.hatchways.io/api/assessment/students 获取数据。现在一切正常,但我不知道如何在这个 API 中获取图片。当我尝试获取图片时,我得到的链接与图片本身相反。我想我需要以某种方式在循环中使用 src 标记,但我不确定如何实现它。我将在下面发布我的代码,并且一如既往地感谢您的帮助,因为我喜欢更多地了解 React。

import React, {Component} from 'react';

class App extends Component{
    constructor(props){
        super(props);
        this.state = {
            items:[],
            isLoaded: false
        };
    }

    componentDidMount(){
      fetch('https://www.hatchways.io/api/assessment/students')
          .then(res=> res.json())
          .then(({ students }) => {
              this.setState({
                  isLoaded: true,
                  items: students,
              })
          });
  }

    render(){
        const {isLoaded, items} = this.state;
        if(!isLoaded){
            return <div>loading data...</div>;
        }

        else{           

            return(
                <div className="Data">
                    <div>
                        {items.map(item=>(
                            <p key={item.id}>
                                 name:{item.firstName +' '+ item.lastName +' '} |
                                 City:{item.city} |
                                 company:{item.company} |
                                 email:{item.email} |
                                 id:{item.id}|
                                 picture:{item.pic}

                            </p>

                        ))};
                    </div>



                </div>

            );
        }

    }

}

export default App;

【问题讨论】:

    标签: javascript json reactjs frontend


    【解决方案1】:

    当我尝试获取图片时,我得到的是与图片本身相反的链接。

    <p key={item.id}>
         name:{item.firstName +' '+ item.lastName +' '} |
         City:{item.city} |
         company:{item.company} |
         email:{item.email} |
         id:{item.id}|
         picture:{item.pic} <<<<<<< You just print it as text.
    </p>
    

    要显示来自 URL 的图像,请使用 img 标记。

    <img src={item.pic}/>
    

    【讨论】:

      【解决方案2】:
      import React, {Component} from 'react';
      
      class App extends Component{
          constructor(props){
              super(props);
              this.state = {
                  items:[],
                  isLoaded: false
              };
          }
      
          componentDidMount(){
            fetch('https://www.hatchways.io/api/assessment/students')
                .then(res=> res.json())
                .then(({ students }) => {
                    this.setState({
                        isLoaded: true,
                        items: students,
                    })
                });
        }
      
          render(){
              const {isLoaded, items} = this.state;
              if(!isLoaded){
                  return <div>loading data...</div>;
              }
      
              else{           
      
                  return(
                      <div className="Data">
                          <div>
                              {items.map(item=>(
                                  <p key={item.id}>
                                       name:{item.firstName +' '+ item.lastName +' '} |
                                       City:{item.city} |
                                       company:{item.company} |
                                       email:{item.email} |
                                       id:{item.id}|
                                       <img src={item.pic}/>
      
                                  </p>
      
                              ))};
                          </div>
      
      
      
                      </div>
      
                  );
              }
      
          }
      
      }
      
      export default App;
      

      【讨论】:

      • 这太明显了!非常感谢您的回答。
      • 如果你不想在段落标签中使用 img 标签,你也可以在段落之后通过将 p 和 img 都包装在一个 div 中来做到这一点
      猜你喜欢
      • 2021-04-09
      • 2019-11-14
      • 2021-05-26
      • 2018-04-24
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多