如何将API数据传递到反应中的状态
在过去的一周中,我一直在学习反应,我发现这真的很有趣。但是,我被卡住了。我正在调用API并使用自定义获取钩获取数据。这是我声明常数并将数据以及其他内容提取的一般函数提取错误并在显示数据之前显示加载程序的一般函数的内容表示的。然后,我将这些数据作为道具传递到另一个称为Animelist的组件,该组件负责映射数据。我现在想做的是将这些数据传递到一个状态,以便以后根据需要对其进行修改。然后,我想将状态作为道具传递给动画派组件,而不是直接数据。我宣布了一个称为topanime的状态。然后,我使用使用效果钩将topanime状态设置为数据。但是,我在控制台上遇到了一个错误,说数据基本上是无效的,我不了解如何进行。我觉得这可能与使用效果钩中的依赖关系阵列有关,但我不确定。任何指导都将不胜感激。以下是我的代码。谢谢。
import { useFetch } from '../hooks/useFetch'
import Error from "./Error"
import Spinner from "../components/spinner/Spinner"
import AnimeList from "../components/animelist/AnimeList"
import Header from '../Header'
import './TopAnime.css'
import { useState } from 'react'
import { useEffect } from 'react'
function TopAnime() {
const [topAnime, setTopAnime] = useState([])
const {data, isPending, error} = useFetch('https://api.jikan.moe/v4/top/anime')
useEffect(() => {
(async () => {
setTopAnime(data);
})();
}, [data]);
return (
<div className="top-anime">
<Header/>
{error && <Error/>}
{isPending && <Spinner/>}
{data && <h1>Top Anime</h1>}
{data && <AnimeList animelist={topAnime}/>}
</div>
)
}
export default TopAnime
这是我收到的错误 在此处输入映像说明
更新:我设法自己修复了以某种方式修复它。我要做的就是添加&amp; amp;就像我为数据所做的那样。我不确定为什么要解决问题,但是解释真的很有帮助。以下是现在的工作代码。
import { useFetch } from '../hooks/useFetch'
import Error from "./Error"
import Spinner from "../components/spinner/Spinner"
import AnimeList from "../components/animelist/AnimeList"
import Header from '../Header'
import './TopAnime.css'
import { useState } from 'react'
import { useEffect } from 'react'
function TopAnime() {
const [topAnime, setTopAnime] = useState([])
const {data, isPending, error} = useFetch('https://api.jikan.moe/v4/top/anime')
useEffect(() => {
(async () => {
setTopAnime(data);
})();
});
return (
<div className="top-anime">
<Header/>
{error && <Error/>}
{isPending && <Spinner/>}
{data && <h1>Top Anime</h1>}
{data && topAnime && <AnimeList animelist={topAnime}/>}
</div>
)
}
export default TopAnime
I have been learning react for the past week and i am finding it really interesting. However, i am stuck. I am making a call to an API and using a custom fetch hook to get the data. This is represented where i have declaring a constant and extracting the data along with other stuff which are general functions to show error and show a loader before the data is displayed. Then i pass this data as a prop into another component called animelist which is responsible for mapping through the data. What i want to do now is to pass this data into a state so i can modify it later as required. I then want to pass the state as a prop to the animelist component rather than the data directly. I have declared a piece of state called topAnime. Then i am using the useEffect hook to set the topAnime state to the data. However i get an error in the console saying that data is basically null which i don't understand how. I feel like it might be something to do with the dependency array in the useEffect hook but i am not sure. Any guidance would be much appreciated. Below is my code. Thanks.
import { useFetch } from '../hooks/useFetch'
import Error from "./Error"
import Spinner from "../components/spinner/Spinner"
import AnimeList from "../components/animelist/AnimeList"
import Header from '../Header'
import './TopAnime.css'
import { useState } from 'react'
import { useEffect } from 'react'
function TopAnime() {
const [topAnime, setTopAnime] = useState([])
const {data, isPending, error} = useFetch('https://api.jikan.moe/v4/top/anime')
useEffect(() => {
(async () => {
setTopAnime(data);
})();
}, [data]);
return (
<div className="top-anime">
<Header/>
{error && <Error/>}
{isPending && <Spinner/>}
{data && <h1>Top Anime</h1>}
{data && <AnimeList animelist={topAnime}/>}
</div>
)
}
export default TopAnime
Here is the error i get
enter image description here
Update: I managed to fix it myself somehow lol. All i had to do was add && topAnime like i have done for data. I am not sure why that fixed it but an explanation would be really helpful. Below is the working code now.
import { useFetch } from '../hooks/useFetch'
import Error from "./Error"
import Spinner from "../components/spinner/Spinner"
import AnimeList from "../components/animelist/AnimeList"
import Header from '../Header'
import './TopAnime.css'
import { useState } from 'react'
import { useEffect } from 'react'
function TopAnime() {
const [topAnime, setTopAnime] = useState([])
const {data, isPending, error} = useFetch('https://api.jikan.moe/v4/top/anime')
useEffect(() => {
(async () => {
setTopAnime(data);
})();
});
return (
<div className="top-anime">
<Header/>
{error && <Error/>}
{isPending && <Spinner/>}
{data && <h1>Top Anime</h1>}
{data && topAnime && <AnimeList animelist={topAnime}/>}
</div>
)
}
export default TopAnime
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑的答案:
我看到您找到了答案。它起作用的原因,如果我没记错的话,是因为当您最初加载apidata时,topanime仍然是无效的,因为呼叫是异步的。基本上,它试图在加载数据之前使用(空的)topanime支柱渲染您的动画派,从而使您有错误的错误。修复它的更好方法(如果您希望更多的组件使用topanime数据)是在申报表开始时检查它,这样:
这将首先检查topanime是否包含数据,如果没有数据,它将显示<强>加载直到它做到。
旧的答案:
如果将API调用移入使用效果,则应该很好。我使用Axios而不是使用Axios进行此示例,然后删除了一些混乱,使您更明显地看到发生了什么。这是Codesandbox链接,您可以在控制台中看到加载的数据: htttps://codesandbox.io/ s/exampleData-bk56bi
Editted answer:
I see that you found your answer. The reason why it works, if I recall correctly, is because when you initially load the apiData, topAnime is still null as the calls are made asynchronously. Basically it tries to render your AnimeList with the (empty) topAnime prop before the Data is loaded, thus giving you the error that it's null. A better way to fix it (if you'd like more components to use the topAnime data) is to check for it at the start of the return, like this:
This will first check if topAnime contains data and if not it will show loading until it does.
Old answer:
If you move the api call inside the useEffect you should be good. I used axios instead of useFetch for this example and I removed some clutter to make it more obvious to see what's happening. Here's the codesandbox link where you can see the data being loaded in the console: https://codesandbox.io/s/examplegetdata-bk56bi