想要从React Js中的API获取数组中的数据

尼丁b

我想从API获取数组中的数据,但无法找到确切的解决方案,我是React.js的新手,我试图从API获取temp数组中的数据,但无法弄清楚如何使用new状态,因为我已经在componentDidMount方法中使用了一个setState。

 Code till componentDidMount method:

   class Apiapp extends Component{
      constructor(){
      super()
      this.state = {
        loading:true,
        characters:{}
     }
    }
     componentDidMount(){
         // This means we can use the setState method as many times are we can depending on what 
        type of methods are we using
        // this.setState({
        //   loading:true
        // })
        fetch("https://pokeapi.co/api/v2/pokemon/5")
            .then(response => response.json())
            .then(data => {
             let tmpArray = []
             for (var i = 0; i < data.game_indices.length; i++) {
                tmpArray.push(data.game_indices[i])
             }
              console.log(data)
              this.setState({
              loading:false,
              characters:data
            })
            this.setState({
              loading:false,
              arrCharacters:tmpArray
            })
          })
      }

     Code of render method:

     render() {
      let text = this.state.loading ? <h2>Loading...</h2> : <div><h2> 
                {this.state.characters.name} 

                </h2>,<h2>{this.state.arrCharacters.name}</h2></div>

               // <h2>{this.state.characters.game_indices[0].version.name}</h2>

                return(<div>{text}</div>)
  }
}

我正在尝试获取“ game_indices”中的所有名称。

API链接:https//pokeapi.co/api/v2/pokemon/ditto

乔纳森·欧文

不要过于复杂

import React, { Component } from 'react';

class ApiApp extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      characters: {},
    };
  }

  async componentDidMount() {
    const data = await fetch('https://pokeapi.co/api/v2/pokemon/5').then((response) => response.json());

    this.setState({ characters: data.game_indices, loading: false });
  }

  render() {
    return <div>{this.state.loading ? <h2>Loading...</h2> : this.state.characters.map((i) => i.version.name)}</div>;
  }
}

我不太确定您要显示什么数据-从问题中不清楚

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

想要从 String 数组中替换 WhiteSpace

来自分类Dev

想要从json数组中获取特定的Element,

来自分类Dev

想要从Windows路径中删除空格

来自分类Dev

想要从每个组中选择数据

来自分类Dev

想要从字典格式的字符串中获取值

来自分类Dev

想要从文件Java中单独获取插入语句

来自分类Dev

想要从我单击它的按钮中获取先前的输入

来自分类Dev

从api获取数据时在React中获取空数组

来自分类Dev

想要从3个表中获取数据同时求和1列

来自分类Dev

在react.js中获取包含数组的数组的数据

来自分类Dev

想要从 Python 中的列表中删除对象本身

来自分类Dev

如何从 JSON API 中的数组对象获取数据?(React.js)

来自分类Dev

想要从我的SVN存储库中删除修订

来自分类Dev

想要从git中的分支删除所有提交

来自分类Dev

想要从注释中删除标记-UIMA RUTA

来自分类Dev

想要从结果中删除没有标签的实体

来自分类Dev

想要从 a 标签中抓取所有特定的 href

来自分类Dev

想要从文件中删除第i个匹配模式

来自分类Dev

想要从我的输出中过滤百分比

来自分类Dev

想要从其他网站复制特定文本在PHP中

来自分类Dev

想要从laravel 5.3中的数据库中获取数据作为单个值,但它以列名返回

来自分类Python

想要从网页获取两个表

来自分类Dev

想要从json读取数据到php文件

来自分类Dev

想要从不同的表中选择数据

来自分类Dev

想要从数据库中获取名字,尽管在MVC应用程序中使用了UserName

来自分类Dev

想要从集会中的数组对象中检索值而不是其他详细信息

来自分类Dev

在React中从api获取数据

来自分类Dev

在 React 中获取 API 数据

来自分类Dev

想要从youtube视频中获取完整尺寸的缩略图

Related 相关文章

热门标签

归档