这是一个包含movie作为键和movie detail作为对象的api现在我已经在useState中获得了movie的值但是我不知道如何准确地获得details
{
"movies": [
{
"Name":"The Forever Purge",
"releasedate":"27 aug 2012",
"id": 1,
"genre" :"Action/Horror/Thriller",
"imageUrl": "https://drive.google.com/file/d/1sisX7wQf6owcj3qys413l471bNu5iM0T/view?
usp=sharing"
},
{
"Name":"Reminiscence",
"releasedate":"27 aug 2012",
"id": 2,
"genre" :"Mystery/Romantic/Sci-Fi",
"imageUrl": "https://drive.google.com/file/d/1GBzbZ6VC243-1kg4EsaYwq6Qm9fLDdJb/view?
usp=sharing"
}
]
}
这是目前我正在写的代码但是这还没有得到movie
的详细信息
const [moviesList, setMoviesList] = useState({});
const getMovies = async () => {
const response = await fetch(
"https://run.mocky.io/v3/55492543-1c03-4b5e-8ff4-28bbd2638780"
);
var data= await response.json();
setMoviesList(data);
// console.log("hiii");
};
useEffect =(() => {
getMovies();
},
[]);
有谁能帮帮我吗
由于您只运行一次getMovies
,因此最好在useEffect
中定义该函数并在那里调用它:
useEffect(() => {
const getMovies = async () => {
const response = await fetch('...');
const data = await response.json();
setMovieList(data);
}
getMovies();
}, []);
这是官方文档推荐的,更多信息:
https://reactjs.org/docs/hooks-faq.html how-can-i-do-data-fetching-with-hooks
https://www.robinwieruch.de/react-hooks-fetch-data/