英文:
Property 'map' does not exist on type 'never' when map through array
问题
I have translated the provided text as requested. Here are the translated parts:
- "getting array from API and storing it into Movie state but when I try to map that movie I got the error:"
- "> 'map' does not exist on type 'never'."
- "the changes that I have applied... I just removed the common API part"
- "consoled data image..."
Please note that the code snippets have not been translated, as per your request.
英文:
getting array from API and storing it into Movie state but when I try to map that movie I got the error:
> "Property 'map' does not exist on type 'never'."
import { useEffect, useState } from "react";
import MoviePoster from "../../Components/MoviePoster";
function Home() {
const [movie, setMovie] = useState();
const API_Url = `https://api.themoviedb.org/3/discover/movie?api_key=${process.env.REACT_APP_TMBD_KEY}`;
useEffect(() => {
getMovies();
}, []);
async function getMovies() {
const api = await fetch(API_Url);
const result = await api.json();
setMovie(result.results);
console.log(result.results);
}
return (
<div>
{movie?.map((data:any) => {
<MoviePoster key={data} {...data} />;
})}
</div>
);
}
export default Home;
> the changes that I have applied... I just removed the common API part
import { useEffect, useState } from "react";
import MoviePoster from "../../Components/MoviePoster";
interface MovieList {
title: string,
id: string
}
function Home() {
const [movie, setMovie] = useState<MovieList[]>([]);
return (
<div>
{movie?.map((data:any) => {
<MoviePoster key={data.id} {...data.title} />;
})}
</div>
);
}
export default Home;
> "consoled data image...."
答案1
得分: 2
I think you need to return from the map
return (
<div>
{movie?.map((data:any) => {
return <MoviePoster key={data} {...data} />;
})}
</div>
);
instead of
return (
<div>
{movie?.map((data:any) => {
<MoviePoster key={data} {...data} />;
})}
</div>
);
在此组件上创建了一个CodeSandbox演示。请在此处查看
英文:
I think you need to return from the map
return (
<div>
{movie?.map((data:any) => {
return <MoviePoster key={data} {...data} />;
})}
</div>
);
instead of
return (
<div>
{movie?.map((data:any) => {
<MoviePoster key={data} {...data} />;
})}
</div>
);
Created a codesandbox demo with this components. Have a look here
答案2
得分: 0
除了Hatana在评论中提到的内容之外,你还可以添加一个加载屏幕。这样,在组件渲染之前,你可以确保将数据存储在数组中。
import { useEffect, useState } from "react";
import MoviePoster from "../../Components/MoviePoster";
interface MovieList {
movieList: [];
}
function Home() {
const [movie, setMovie] = useState<MovieList>();
const [loading, setLoading] = useState(true);
const API_Url = `https://api.themoviedb.org/3/discover/movie?
api_key=${process.env.REACT_APP_TMBD_KEY}`;
useEffect(() => {
getMovies();
setLoading(false);
}, []);
async function getMovies() {
const api = await fetch(API_Url);
const result = await api.json();
setMovie(result.results);
console.log(result.results);
}
if (loading) {
return <div>Loading</div>;
}
return (
<div>
{movie?.map((data: any) => (
<MoviePoster key={data} {...data} />;
))}
</div>
);
}
export default Home;
英文:
In addtion to what has been said by Hatana in the comments, you could also add a loading screen. That way you can be sure that you have your data in your array before the component is rendered.
import { useEffect, useState } from "react";
import MoviePoster from "../../Components/MoviePoster";
interface MovieList {
movieList: [];
}
function Home() {
const [movie, setMovie] = useState<MovieList>();
const [loading, setLoading] = useState(true);
const API_Url = `https://api.themoviedb.org/3/discover/movie?
api_key=${process.env.REACT_APP_TMBD_KEY}`;
useEffect(() => {
getMovies();
setLoading(false);
}, []);
async function getMovies() {
const api = await fetch(API_Url);
const result = await api.json();
setMovie(result.results);
console.log(result.results);
}
if (loading) {
return <div>Loading</div>;
}
return (
<div>
{movie?.map((data: any) => {
<MoviePoster key={data} {...data} />;
})}
</div>
);
export default Home;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论