简体   繁体   English

ReactJS“未处理的拒绝(TypeError):this.state.features.map不是函数”

[英]ReactJS “Unhandled Rejection (TypeError): this.state.features.map is not a function”

I'm learning React and now i'm trying do a get resquest and list with map but when I run this code they come up with this error "Unhandled Rejection (TypeError): this.state.features.map is not a function". 我正在学习React,现在我尝试获取获取请求并使用map进行列表,但是当我运行此代码时,他们会遇到此错误“未处理的拒绝(TypeError):this.state.features.map不是函数” 。 I already search this but I'm not undertand what its going on. 我已经搜索过了,但我不知道它在发生什么。

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

class App extends Component {

  constructor() {
    super();
    this.state = {
      features: [{
        id: 1,
        name: 'Test',
        count: 1
      }]
    }
  }

  componentWillMount() {
    fetch("http://demo6085176.mockable.io/features")
      .then(response => response.json())
      .then(json => {
        console.log(json);
        this.setState({
          features: json,
        });
      });
    console.log(this.state.features)

  }

  render() {
    return (
      <div className="App">
        <ul>
          {
            this.state.features.map(function(feature){
              return (
                <li key={feature.id}><button type="button">Upvote</button> ({feature.count}) <span>{feature.name}</span></li>
              )
            })
          }
        </ul>
      </div>
    );
  }
}

export default App;

In your componentWillMount, just do this: 在您的componentWillMount中,只需执行以下操作:

componentWillMount() {
   fetch("http://demo6085176.mockable.io/features")
    .then(response => response.json())
    .then(json => {
      console.log(json);
      this.setState({ features: json.features });
   });
}

The response you get from the API is an object which has a key of features which is an array of objects of the data you want. 您从API获得的响应是​​一个具有关键features的对象,该features是所需数据的对象数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应错误:未处理的拒绝(类型错误):this.state.categories.map 不是 function - React error : Unhandled Rejection (TypeError): this.state.categories.map is not a function 未处理的拒绝(TypeError):conversations.map 不是 function - Unhandled Rejection (TypeError): conversations.map is not a function ReactJS:未处理的拒绝(TypeError)304 - ReactJS: Unhandled Rejection (TypeError) 304 映射函数不起作用未处理的拒绝(类型错误):fieldsList.map 不是函数 - Map function not working Unhandled Rejection (TypeError): fieldsList.map is not a function 在 ReactJS 中获取错误为“未处理的拒绝(TypeError):that.setState 不是函数” - Getting error as “Unhandled Rejection (TypeError): that.setState is not a function” in ReactJS 未处理的拒绝(TypeError):setToken 不是函数 - Unhandled Rejection (TypeError): setToken is not a function 未处理的拒绝 (TypeError):setDailyData 不是函数 - Unhandled Rejection (TypeError): setDailyData is not a function 未处理的拒绝(TypeError):setX 不是 function - Unhandled Rejection (TypeError): setX is not a function 未处理的拒绝(TypeError):Object(...) 不是函数 - Unhandled Rejection (TypeError): Object(...) is not a function 未处理的拒绝(TypeError):state.cartItems 未定义 - Unhandled Rejection (TypeError): state.cartItems is undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM