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

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

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'."

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...."

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

答案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 (
    &lt;div&gt;
      {movie?.map((data:any) =&gt; {
        return &lt;MoviePoster key={data} {...data} /&gt;;
      })}
    &lt;/div&gt;
  );

instead of

return (
    &lt;div&gt;
      {movie?.map((data:any) =&gt; {
        &lt;MoviePoster key={data} {...data} /&gt;;
      })}
    &lt;/div&gt;
  );

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 &quot;react&quot;;
import MoviePoster from &quot;../../Components/MoviePoster&quot;;

interface MovieList {
  movieList: [];
}

function Home() {
  const [movie, setMovie] = useState&lt;MovieList&gt;();
  const [loading, setLoading] = useState(true);
  const API_Url = `https://api.themoviedb.org/3/discover/movie? 
api_key=${process.env.REACT_APP_TMBD_KEY}`;

useEffect(() =&gt; {
  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 &lt;div&gt;Loading&lt;/div&gt;;
}

return (
  &lt;div&gt;
    {movie?.map((data: any) =&gt; {
      &lt;MoviePoster key={data} {...data} /&gt;;
    })}
  &lt;/div&gt;
);

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:

确定