属性 ‘map’ 在类型 ‘never’ 上不存在,当遍历数组时发生此错误。

huangapple go评论124阅读模式
英文:

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:

  1. "getting array from API and storing it into Movie state but when I try to map that movie I got the error:"
  2. "> 'map' does not exist on type 'never'."
  3. "the changes that I have applied... I just removed the common API part"
  4. "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'."

  1. import { useEffect, useState } from "react";
  2. import MoviePoster from "../../Components/MoviePoster";
  3. function Home() {
  4. const [movie, setMovie] = useState();
  5. const API_Url = `https://api.themoviedb.org/3/discover/movie?api_key=${process.env.REACT_APP_TMBD_KEY}`;
  6. useEffect(() => {
  7. getMovies();
  8. }, []);
  9. async function getMovies() {
  10. const api = await fetch(API_Url);
  11. const result = await api.json();
  12. setMovie(result.results);
  13. console.log(result.results);
  14. }
  15. return (
  16. <div>
  17. {movie?.map((data:any) => {
  18. <MoviePoster key={data} {...data} />;
  19. })}
  20. </div>
  21. );
  22. }
  23. export default Home;

> the changes that I have applied... I just removed the common API part

  1. import { useEffect, useState } from "react";
  2. import MoviePoster from "../../Components/MoviePoster";
  3. interface MovieList {
  4. title: string,
  5. id: string
  6. }
  7. function Home() {
  8. const [movie, setMovie] = useState<MovieList[]>([]);
  9. return (
  10. <div>
  11. {movie?.map((data:any) => {
  12. <MoviePoster key={data.id} {...data.title} />;
  13. })}
  14. </div>
  15. );
  16. }
  17. export default Home;

> "consoled data image...."

属性 ‘map’ 在类型 ‘never’ 上不存在,当遍历数组时发生此错误。

答案1

得分: 2

I think you need to return from the map

  1. return (
  2. <div>
  3. {movie?.map((data:any) => {
  4. return <MoviePoster key={data} {...data} />;
  5. })}
  6. </div>
  7. );

instead of

  1. return (
  2. <div>
  3. {movie?.map((data:any) => {
  4. <MoviePoster key={data} {...data} />;
  5. })}
  6. </div>
  7. );

在此组件上创建了一个CodeSandbox演示。请在此处查看

英文:

I think you need to return from the map

  1. return (
  2. &lt;div&gt;
  3. {movie?.map((data:any) =&gt; {
  4. return &lt;MoviePoster key={data} {...data} /&gt;;
  5. })}
  6. &lt;/div&gt;
  7. );

instead of

  1. return (
  2. &lt;div&gt;
  3. {movie?.map((data:any) =&gt; {
  4. &lt;MoviePoster key={data} {...data} /&gt;;
  5. })}
  6. &lt;/div&gt;
  7. );

Created a codesandbox demo with this components. Have a look here

答案2

得分: 0

除了Hatana在评论中提到的内容之外,你还可以添加一个加载屏幕。这样,在组件渲染之前,你可以确保将数据存储在数组中。

  1. import { useEffect, useState } from "react";
  2. import MoviePoster from "../../Components/MoviePoster";
  3. interface MovieList {
  4. movieList: [];
  5. }
  6. function Home() {
  7. const [movie, setMovie] = useState<MovieList>();
  8. const [loading, setLoading] = useState(true);
  9. const API_Url = `https://api.themoviedb.org/3/discover/movie?
  10. api_key=${process.env.REACT_APP_TMBD_KEY}`;
  11. useEffect(() => {
  12. getMovies();
  13. setLoading(false);
  14. }, []);
  15. async function getMovies() {
  16. const api = await fetch(API_Url);
  17. const result = await api.json();
  18. setMovie(result.results);
  19. console.log(result.results);
  20. }
  21. if (loading) {
  22. return <div>Loading</div>;
  23. }
  24. return (
  25. <div>
  26. {movie?.map((data: any) => (
  27. <MoviePoster key={data} {...data} />;
  28. ))}
  29. </div>
  30. );
  31. }
  32. 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.

  1. import { useEffect, useState } from &quot;react&quot;;
  2. import MoviePoster from &quot;../../Components/MoviePoster&quot;;
  3. interface MovieList {
  4. movieList: [];
  5. }
  6. function Home() {
  7. const [movie, setMovie] = useState&lt;MovieList&gt;();
  8. const [loading, setLoading] = useState(true);
  9. const API_Url = `https://api.themoviedb.org/3/discover/movie?
  10. api_key=${process.env.REACT_APP_TMBD_KEY}`;
  11. useEffect(() =&gt; {
  12. getMovies();
  13. setLoading(false);
  14. }, []);
  15. async function getMovies() {
  16. const api = await fetch(API_Url);
  17. const result = await api.json();
  18. setMovie(result.results);
  19. console.log(result.results);
  20. }
  21. if (loading) {
  22. return &lt;div&gt;Loading&lt;/div&gt;;
  23. }
  24. return (
  25. &lt;div&gt;
  26. {movie?.map((data: any) =&gt; {
  27. &lt;MoviePoster key={data} {...data} /&gt;;
  28. })}
  29. &lt;/div&gt;
  30. );
  31. export default Home;

huangapple
  • 本文由 发表于 2023年7月6日 14:02:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定