【问题标题】:React Error - Cannot read property 'state' of undefined反应错误 - 无法读取未定义的属性“状态”
【发布时间】:2018-04-27 16:31:16
【问题描述】:

我正在关注这个关于使用 fetch 的教程;我能够使用这个模拟 API 提取照片:https://randomuser.me/api/ ...但是修改了代码以从 API 中提取其他数据,例如名。但是,在这个过程中遇到了一个错误:

代码如下:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {

  constructor(){
    super();
    this.state = {
      pictures: [],
      people: [],
    };
  }

  componentWillMount()
  {
    fetch('https://randomuser.me/api/?results=10')
    .then(results => results.json())
    .then(data => this.setState({ pictures: data.results }))
    .then(data => this.setState({ people: data.results }));
  }



    render()  {
      console.log(this.state);
      return (
        <div>
            {
              this.state.pictures.map( pic =>(<div><img src={pic.picture.medium} /></div>) ).
              this.state.people.map(person => (<div>{person.name.first}</div>))
            }    
        </div>
      );
    }
  }

export default App;

基本上,我试图弄清楚如何提取更多图片(来自 API 的其他数据),但不确定我是否走在正确的轨道上。目前,我正在尝试获取名字。请问我能得到一些帮助吗?

【问题讨论】:

    标签: javascript reactjs api components


    【解决方案1】:

    每个表达式都应该用{}括起来。

    {this.state.pictures.map(pic => (
      <div>
        <img src={pic.picture.medium} />
      </div>
    ))}
    {this.state.people.map(person => <div>{person.name.first}</div>)}
    

    【讨论】:

      【解决方案2】:

      鉴于 API 响应中没有任何名为 picturespeople 的内容,我认为您使用的变量名称没有意义。

      您要做的是将数据设置为状态,然后对其进行循环。

      简而言之,你会有这样的东西:

      class App extends Component {
        state = { data: [] }
      
        componentWillMount() {
          fetch('https://randomuser.me/api/?results=10')
            .then(results => results.json())
            .then(data => this.setState({ data: data.results }))
        }
      
        render() {
          if (!this.state.data) {
            return 'loading'
          }
          console.log(this.state.data)
          return (
            <div>
              {
                this.state.data.map(item => (
                  <div>
                    <img src={item.picture.medium} />
                    <p>{item.name.first}</p>
                  </div>
                ))
              }
            </div>
          );
        }
      }
      
      export default App;
      

      有一个工作示例here

      【讨论】:

        【解决方案3】:

        响应data.results 是一个对象数组,其中每个对象都包含name 属性。您需要将渲染函数更改为显示名称

        render()  {
          return (
            <div>
                {
                  this.state.pictures.map(item =>(
                  <div>
                    <img src={item.picture.medium} />
                    <div>{item.name.first}</div>
                  </div>))
                }    
            </div>
          );
        }
        

        您用于存储结果的名称pictures不合适,请注意命名部分。

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 2022-07-26
          • 1970-01-01
          • 2018-04-12
          • 2015-08-19
          相关资源
          最近更新 更多