`.map` 方法在 React 应用中不起作用。

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

Method .map don't work in application React

问题

对象的 .map 方法不起作用,我无法与导入的 API 对象交互。在浏览器控制台上,我读到了这个对象。

这是一个名为 "razze" 的组件。

const BASE_URL = "https://www.dnd5eapi.co/api/races";

const ListaRazze = () => {
  const [razze, setRazze] = useState<RazzeArray>();

  useEffect(() => {
    fetch(BASE_URL)
      .then((res) => res.json())
      .then((results) => {
        console.log('result', results);
        setRazze(results);

        const prova = Object.values(results);
      })
      .catch((error) => {
        console.error('Error:', error);
      });
  }, []);

  return (
    <>
      {razze && razze.razze ? (
        razze.razze.map(({ indice, name, url }) => (
          <div key={indice}>{name} {url}</div>
        ))
      ) : (
        <div>Loading...</div>
      )}
    </>
  );
};

export default ListaRazze;

这是一个位于 models 包中的接口:

export interface RazzeArray {
    count: number
    razze: Razza[]
}
  
export interface Razza {
    indice: number
    name: string
    url: string
}

能够进入这个对象。

英文:

the .map method of the object doesn't work, I can't interact with the object of the API I imported. On the console of browser i read the object.

This is a component razze("races")

const BASE_URL = &quot;https://www.dnd5eapi.co/api/races&quot;;



const ListaRazze = () =&gt; {
  const [razze, setRazze] = useState&lt;RazzeArray&gt;();

  useEffect(() =&gt; {
    fetch(BASE_URL)
      .then((res) =&gt; res.json())
      .then((results) =&gt; {
        console.log(&#39;result&#39;, results);
        setRazze(results);

        const prova = Object.values(results);
      })
      .catch((error) =&gt; {
        console.error(&#39;Error:&#39;, error);
      });
  }, []);

  return (
    
     &lt;&gt;
      {razze &amp;&amp; razze.razze ? (
        razze.razze.map(({ indice, name, url }) =&gt; (
          &lt;div key={indice}&gt;{name} {url}&lt;/div&gt;
        ))
      ) : (
        &lt;div&gt;Loading...&lt;/div&gt;
      )}
    &lt;/&gt;
  );
};

export default ListaRazze;

This is interface in a package models

export interface RazzeArray {
    count: number
    razze: Razza[]
  }
  
  export interface Razza {
    indice: number
    name: string
    url: string
  }

Being able to enter the object

答案1

得分: 0

接收到的数据与接口中期望的数据不同。请查看您的 api result 的属性并匹配属性名称。

我还建议您保持代码使用英文,以确保一致性和清晰度。

export interface IRaceList {
  count: number
  results: IRace[]
}
  
export interface IRace {
  index: number
  name: string
  url: string
}

为您的状态设置一个初始值。这也会使您的代码更加可预测和一致。如果您不想要初始状态,请确保类型反映这一点。

const RaceList = () => {
  const [raceList, setRaceList] = useState<IRaceList>({
    count: 0,
    results: []
  });

  useEffect(() => {
    fetch(BASE_URL)
      .then((res) => res.json())
      .then((results: IRaceList) => {
        setRaceList(results);
      })
      .catch((error) => {
        console.error('Error:', error);
      });
  }, []);

  return (
    <>
      {raceList.count ? (
        raceList.results.map(({ index, name, url }) => (
          <div key={index}>{name} {url}</div>
        ))
      ) : (
        <div>Loading...</div>
      )}
    </>
  );
};
英文:

The data you're receiving is different from the data you're expecting in your interfaces. Observe the properties of your api result and match the property names.

I'd also recommend that you keep your code in English for consistency and clarity.

export interface IRaceList {
  count: number
  results: IRace[]
}
  
export interface IRace {
  index: number
  name: string
  url: string
}

Set an initial value for your state. This too will make your code more predictable and consistant. If you don't want an initial state, then make sure the types reflect that.

const RaceList = () =&gt; {
  const [raceList, setRaceList] = useState&lt;IRaceList&gt;({
    count: 0,
    results: []
  });

  useEffect(() =&gt; {
    fetch(BASE_URL)
      .then((res) =&gt; res.json())
      .then((results: IRaceList) =&gt; {
        setRaceList(results);
      })
      .catch((error) =&gt; {
        console.error(&#39;Error:&#39;, error);
      });
  }, []);

  return (
     &lt;&gt;
      {raceList.count ? (
        raceList.results.map(({ index, name, url }) =&gt; (
          &lt;div key={index}&gt;{name} {url}&lt;/div&gt;
        ))
      ) : (
        &lt;div&gt;Loading...&lt;/div&gt;
      )}
    &lt;/&gt;
  );
};

huangapple
  • 本文由 发表于 2023年6月12日 21:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457360.html
匿名

发表评论

匿名网友

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

确定