<bdo id='jqenR'></bdo><ul id='jqenR'></ul>
      1. <tfoot id='jqenR'></tfoot>

        <i id='jqenR'><tr id='jqenR'><dt id='jqenR'><q id='jqenR'><span id='jqenR'><b id='jqenR'><form id='jqenR'><ins id='jqenR'></ins><ul id='jqenR'></ul><sub id='jqenR'></sub></form><legend id='jqenR'></legend><bdo id='jqenR'><pre id='jqenR'><center id='jqenR'></center></pre></bdo></b><th id='jqenR'></th></span></q></dt></tr></i><div id='jqenR'><tfoot id='jqenR'></tfoot><dl id='jqenR'><fieldset id='jqenR'></fieldset></dl></div>

        <small id='jqenR'></small><noframes id='jqenR'>

        <legend id='jqenR'><style id='jqenR'><dir id='jqenR'><q id='jqenR'></q></dir></style></legend>

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

        时间:2023-10-02

        • <bdo id='f8qJx'></bdo><ul id='f8qJx'></ul>

          <tfoot id='f8qJx'></tfoot>

        • <legend id='f8qJx'><style id='f8qJx'><dir id='f8qJx'><q id='f8qJx'></q></dir></style></legend>

          <i id='f8qJx'><tr id='f8qJx'><dt id='f8qJx'><q id='f8qJx'><span id='f8qJx'><b id='f8qJx'><form id='f8qJx'><ins id='f8qJx'></ins><ul id='f8qJx'></ul><sub id='f8qJx'></sub></form><legend id='f8qJx'></legend><bdo id='f8qJx'><pre id='f8qJx'><center id='f8qJx'></center></pre></bdo></b><th id='f8qJx'></th></span></q></dt></tr></i><div id='f8qJx'><tfoot id='f8qJx'></tfoot><dl id='f8qJx'><fieldset id='f8qJx'></fieldset></dl></div>

            <small id='f8qJx'></small><noframes id='f8qJx'>

                  <tbody id='f8qJx'></tbody>

                  本文介绍了ReactJS“未处理的拒绝(TypeError):this.state.features.map 不是函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在学习 React,现在我正在尝试使用 map 执行 get 请求和列表,但是当我运行此代码时,他们会遇到此错误Unhandled Rejection (TypeError): this.state.features.map不是函数".我已经搜索过这个,但我不明白发生了什么.

                  I'm learning React and now I'm trying to do a get request 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". I have already searched this but I do not understand what is 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;
                  

                  推荐答案

                  在你的componentWillMount中,这样做:

                  In your componentWillMount, just do this:

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

                  您从 API 获得的响应是一个对象,该对象具有 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.

                  这篇关于ReactJS“未处理的拒绝(TypeError):this.state.features.map 不是函数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:返回 HTML 而不是 JSON 的 Google Apps 脚本的内容服务 下一篇:如何在 fetch/axios 跨站请求中使用 JSONP

                  相关文章

                    <legend id='YXTsv'><style id='YXTsv'><dir id='YXTsv'><q id='YXTsv'></q></dir></style></legend>
                      <bdo id='YXTsv'></bdo><ul id='YXTsv'></ul>

                    <small id='YXTsv'></small><noframes id='YXTsv'>

                    <tfoot id='YXTsv'></tfoot>

                    <i id='YXTsv'><tr id='YXTsv'><dt id='YXTsv'><q id='YXTsv'><span id='YXTsv'><b id='YXTsv'><form id='YXTsv'><ins id='YXTsv'></ins><ul id='YXTsv'></ul><sub id='YXTsv'></sub></form><legend id='YXTsv'></legend><bdo id='YXTsv'><pre id='YXTsv'><center id='YXTsv'></center></pre></bdo></b><th id='YXTsv'></th></span></q></dt></tr></i><div id='YXTsv'><tfoot id='YXTsv'></tfoot><dl id='YXTsv'><fieldset id='YXTsv'></fieldset></dl></div>