如何修复 data.map 不是函数 api
How to fix the data.map not a function api
我正在尝试获取并显示 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
});
我看到您的代码运行良好,您犯的唯一错误是 json
变量不是数组。它有一个 results
键,您需要在其中映射:
查看此日志:
所以你需要这样做:
const json = await response.json();
this.setState({
data: json.results,
isLoading: false
});
而不是仅使用 data
。
这是我创建的用于查看错误的演示沙盒:https://codesandbox.io/s/sleepy-jang-psedq
希望对您有所帮助。
我正在尝试获取并显示 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
});
我看到您的代码运行良好,您犯的唯一错误是 json
变量不是数组。它有一个 results
键,您需要在其中映射:
查看此日志:
所以你需要这样做:
const json = await response.json();
this.setState({
data: json.results,
isLoading: false
});
而不是仅使用 data
。
这是我创建的用于查看错误的演示沙盒:https://codesandbox.io/s/sleepy-jang-psedq
希望对您有所帮助。