javascript - 如何修复 data.map 而不是函数 api

标签 javascript arrays reactjs api pokemon-go

我正在尝试获取并显示 api 的数据。

https://pokeapi.co/api/v2/pokemon/1

我尝试显示数据,但不起作用。

PokemonSpecies.js

import React from "react";
// import Loader from "./Loader";
import { Card, Col } from "react-bootstrap";

import { Container, Row } from "react-bootstrap";
import CardPokemon from "./containers/CardPokemon";

class PokemonSpecies extends React.Component {
  state = {
    data: [],
    isLoading: false,
    abilities: []
  };

  async componentDidMount() {
    const id = this.props.match.params.id;

    this.setState({ isLoading: true });
    try {
      const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
      const json = await response.json();
      this.setState({
        data: json,
        isLoading: false
      });
      console.log({ json });
    } catch (err) {
      console.log(err.msg);
      this.setState({ isLoading: false });
      throw err;
    }
  }

  render() {
    const { data } = this.state;
    return (
      <div className="Pokemon">
        <Container>
          <Row>
            <Col lg="4"></Col>
            <Col lg="4">
              <CardPokemon data={data} />
            </Col>
            <Col lg="4"></Col>
          </Row>
          <Row></Row>
        </Container>
      </div>
    );
  }
}

export default PokemonSpecies;

CardPokemon.js

import React from "react";

import DataSinglePokemon from "./DataSinglePokemon";

const CardPokemon = ({ data }) => {
  return (
    <>
      {data.map((info, index) => (
        <DataSinglePokemon
          key={info + index}
          id={info.id}
          name={info.name
            .toLowerCase()
            .split(" ")
            .map(letter => letter.charAt(0).toUpperCase() + letter.substring(1))
            .join(" ")}
          height={info.height}
          {...info}
        />
      ))}
    </>
  );
};

export default CardPokemon;

DataSinglePokemon.js

import React from "react";
import { Card, Col } from "react-bootstrap";
import "../../App.css";
const DataSinglePokemon = props => {
  const { height, name, id } = props;

  const urlImage = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png?raw=true`;
  return (
    <Col lg={3}>
      <Card>
        <Card.Header>{id}</Card.Header>
        <Card.Body className="mx-auto">
          <Card.Title> {name} </Card.Title>
          <Card.Text>
            <img alt={name} src={urlImage} />
            <br></br>
            Taille : {height}
          </Card.Text>
        </Card.Body>
      </Card>
    </Col>
  );
};

export default DataSinglePokemon;

我有 json,但是当我尝试显示 Pokemon 的名称或能力时,我遇到了这个错误我已经尝试了很多东西,但我是 React js 的新手...:

TypeError: data.map is not a function
CardPokemon
src/components/containers/CardPokemon.js:7
   4 | 
   5 | const CardPokemon =({data}) =>{
   6 |     
>  7 |   return(
   8 |   
   9 |   <>
  10 |      {data.map((info,index) =>(

最佳答案

我猜问题出在下面一行。您能否检查一下 API 调用的响应。

const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
const json = await response.json();
   this.setState({ 
      data: json,  // Check the data type of json. It should be in Array.
      isLoading: false
   });

关于javascript - 如何修复 data.map 而不是函数 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58658859/

相关文章:

javascript - Material-UI Select,如何应用 :focus-within styling on the select when the input is clicked?

Javascript获取视口(viewport)高度而不溢出Y

Javascript 获取函数上下文

Javascript 表字符串到数组

javascript - AngularJS 比较两个(作用域)数组并且只发布 'non-same' 结果(2018)

JavaScript:创建一个返回数组频率分布的函数

javascript - 在函数中调用 "setState"作为回调,在没有任何新状态值的情况下如何成功更新状态?

reactjs - 在 React 中,第一次出现在页面上时以不同方式呈现组件的最佳方式是什么?

javascript - 如何使用 jQuery 更改我的两个 td 的背景颜色?

javascript - 使用 jQuery 动态添加表格行和列