【发布时间】:2021-07-25 07:33:54
【问题描述】:
我正在尝试将我的应用与 API 连接起来。它适用于类,但我尝试使用钩子。
这是错误
未处理的拒绝(TypeError):无法读取未定义的属性“协议”
这是我的代码
import React, { useState, useEffect } from "react";
import axios from "axios";
import PokemonCard from "./PokemonCard";
const Home = () => {
const [currentState, setNewState] = useState({
url: "https://pokeapi.co/api/v2/pokemon?limit=600",
pokemons: null,
});
useEffect(() => {
getApi();
async function getApi() {
const res = await axios.get(currentState.url);
setNewState({ pokemons: res.data["results"] });
}
});
return (
<>
{currentState.pokemons ? (
<div className="pokemonCards">
{currentState.pokemons.map((poke) => (
<PokemonCard key={poke.name} name={poke.name} url={poke.url} />
))}
</div>
) : (
<h1> Loading </h1>
)}
</>
);
};
【问题讨论】:
-
这应该对您有所帮助。 stackoverflow.com/a/55247471/2138752
标签: reactjs axios react-hooks