英文:
React Hook useState doesnt work when I try passing in a JSON object fetched from an API request
问题
错误:
无法读取未定义的属性(读取 'ranking')
类型错误:无法读取未定义的属性(读取 'ranking')
在 MovieCard (http://localhost:3000/static/js/bundle.js:250:44) 中
在 renderWithHooks (http://localhost:3000/static/js/bundle.js:21333:22) 中
在 mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:24619:17) 中
在 beginWork (http://localhost:3000/static/js/bundle.js:25915:20) 中
在 HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:10925:18) 中
在 Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:10969:20) 中
在 invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:11026:35) 中
在 beginWork$1 (http://localhost:3000/static/js/bundle.js:30900:11) 中
在 performUnitOfWork (http://localhost:3000/static/js/bundle.js:30147:16) 中
在 workLoopSync (http://localhost:3000/static/js/bundle.js:30070:9) 中
我的 app.jsx
import React, { useEffect, useState} from "react";
// import axios from "axios";
import "./App.css";
import SearchIcon from './search.svg';
import MovieCard from "./MovieCard";
const API_URL = 'https://anime-db.p.rapidapi.com/anime?page=1&size=10';
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '86f93f3665msh3f8b81058a26p1a3effjsnfgg0c6ff789ce',
'X-RapidAPI-Host': 'anime-db.p.rapidapi.com'
}
};
const App = () => {
const [anime,setAnime] = useState([]);
const searchAnime = async (title)=>{
const response = await fetch(${API_URL}&search=${title}
,options);
const data =await response.json();
console.log(data);
data.then(function(res){
setAnime(res.data)
})
}
useEffect(()=>{
searchAnime("shigatsu");
},[])
return (<div className="app">
<h1>
Annie-Plex
</h1>
<div className="search">
<input placeholder="搜索动漫" value = "" onChange={()=>{}}/>
<img
src={SearchIcon}
alt="搜索"
onClick={()=>{}}
/>
</div>
<div className="container">
<MovieCard
M1 = {anime[0]}/> //此部分出现错误
</div>
</div>);
};
export default App;
我的 moviecard.js
import React from "react";
const MovieCard = ({M1}) => {
return (
<div className="movie">
<div>
<p>当前排名: {M1.ranking}</p>
</div>
<div>
<img src={M1.image} alt={M1.title} />
</div>
<div>
<span>{M1.type}</span>
<h3>{M1.title}</h3>
</div>
</div>
)
}
export default MovieCard;
我期望能加载请求的第一张卡片。
英文:
Error:
Cannot read properties of undefined (reading 'ranking')
TypeError: Cannot read properties of undefined (reading 'ranking')
at MovieCard (http://localhost:3000/static/js/bundle.js:250:44)
at renderWithHooks (http://localhost:3000/static/js/bundle.js:21333:22)
at mountIndeterminateComponent (http://localhost:3000/static/js/bundle.js:24619:17)
at beginWork (http://localhost:3000/static/js/bundle.js:25915:20)
at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:10925:18)
at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:10969:20)
at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:11026:35)
at beginWork$1 (http://localhost:3000/static/js/bundle.js:30900:11)
at performUnitOfWork (http://localhost:3000/static/js/bundle.js:30147:16)
at workLoopSync (http://localhost:3000/static/js/bundle.js:30070:9)
My app.jsx
import React, { useEffect, useState} from "react";
// import axios from "axios";
import "./App.css";
import SearchIcon from './search.svg';
import MovieCard from "./MovieCard";
const API_URL = 'https://anime-db.p.rapidapi.com/anime?page=1&size=10';
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '86f93f3665msh3f8b81058a26p1a3effjsnfgg0c6ff789ce',
'X-RapidAPI-Host': 'anime-db.p.rapidapi.com'
}
};
const App = () => {
const [anime,setAnime] = useState([]);
const searchAnime = async (title)=>{
const response = await fetch(`${API_URL}&search=${title}`,options);
const data =await response.json();
console.log(data);
data.then(function(res){
setAnime(res.data)
})
}
useEffect(()=>{
searchAnime("shigatsu");
},[])
return (<div className="app">
<h1>
Annie-Plex
</h1>
<div className="search">
<input placeholder="Search for Anime" value = "" onChange={()=>{}}/>
<img
src={SearchIcon}
alt="Search"
onClick={()=>{}}
/>
</div>
<div className="container">
<MovieCard
M1 = {anime[0]}/> //this part gives the error
</div>
</div>);
};
export default App;
my moviecard.js
import React from 'react'
const MovieCard = ({M1}) => {
return (
<div className="movie">
<div>
<p>Current Ranking: {M1.ranking}</p>
</div>
<div>
<img src={M1.image} alt={M1.title} />
</div>
<div>
<span>{M1.type}</span>
<h3>{M1.title}</h3>
</div>
</div>
)
}
export default MovieCard
I was expecting the first card of the request to be loaded up
答案1
得分: 1
不要在请求完成并且数组中实际存在第一个元素之前显示该组件。
{anime[0] && <MovieCard M1={anime[0]}/>}
此外,不要在已经使用了await
的值上使用.then
(它不是Promise
)。如果返回的JSON是一个数组,那么你应该简单地调用setAnime(data);
。
英文:
Don't display the component until the request has been completed and there actually is a first element in the array.
{anime[0] && <MovieCard M1={anime[0]}/>}
Also, don't use .then
on a value that has already been await
ed (it's not a Promise
). If the returned JSON is an array, then you should simply call setAnime(data);
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论