简体   繁体   English

我在 React 应用程序上显示我的 api 数据时遇到问题

[英]I have trouble displaying my api data on my react app

Im building a react app that fetches a random food data from spoonacular.com.我正在构建一个反应应用程序,它从spoonacular.com 获取随机食物数据。 Im trying to display the title name of the food on the page but it doesn't show up and also why does it keep fetching a bunch of different data as shown in the picture of the console.log even though I specified the number of data to fetch as 1 in the URL我试图在页面上显示食物的标题名称,但它没有显示出来,以及为什么即使我指定了数据数量,它也会继续获取一堆不同的数据,如 console.log 的图片所示在 URL 中获取为 1

This is my Home.js这是我的 Home.js

import React, { useEffect, useState } from "react";
import axios from "axios";
import Recipe from "../components/Recipes";

const URL = `https://api.spoonacular.com/recipes/random?apiKey=${APIKey}&number=1`;

function Home() {
  const [food, setFood] = useState({});
  useEffect(() => {
    axios
      .get(URL)
      .then(function (response) {
        setFood(response.data);
        console.log(food);
      })
      .catch(function (error) {
        console.warn(error);
      });
  }, [food]);

  return (
    <main>
      <Recipe recipeList={food} />
    </main>
  );
}

export default Home;

and this is my Recipe.js component这是我的 Recipe.js 组件

import React from "react";

function Recipe({ recipeList }) {
  return (
    <div className="recipeCard">
      <h1>{recipeList.title}</h1>
    </div>
  );
}

export default Recipe;

and this is the picture of the console when I log the results fetched from the API (they're all different food datas but I only wanted to fetch 1 food data and display it on the page)这是我记录从 API 获取的结果时控制台的图片(它们都是不同的食物数据,但我只想获取 1 个食物数据并将其显示在页面上) 在此处输入图片说明

That's right, you get 1 random recipe, but useEffect works every time you update the food state, so you have an infinite loop.没错,你会得到 1 个随机配方,但是每次更新food状态时useEffect起作用,所以你有一个无限循环。 Just remove food from useEffect dependency.只需从useEffect依赖项中删除food useEffect It's also better to check if recipeList exists so you don't get a missing title error This should work as expected: Home.js:最好检查 recipeList 是否存在,以免出现缺少标题错误这应该按预期工作:Home.js:

import React, { useEffect, useState } from "react";
import axios from "axios";
import Recipe from "../components/Recipes";

const URL = `https://api.spoonacular.com/recipes/random?apiKey=${APIKey}&number=1`;

function Home() {
  const [food, setFood] = useState(null);
  useEffect(() => {
    axios
      .get(URL)
      .then(function (response) {
        setFood(response.data);
        console.log(food);
      })
      .catch(function (error) {
        console.warn(error);
      });
  }, []);

  return (
    <main>
      <Recipe recipeList={food} />
    </main>
  );
}

export default Home;

Recipe.js:食谱.js:

import React from "react";

function Recipe({ recipeList }) {

  if(!recipeList) return <></>
  return (
    <div className="recipeCard">
      <h1>{recipeList?.title}</h1>
    </div>
  );
}

export default Recipe;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 Api 数据未显示在我的 React 应用程序上 - My Api data is not displaying on my react app Django/React 应用程序:如何在我的 web 应用程序中兼容地同时拥有 Django URLS(用于 api 发送数据)和 React URLS(用于呈现我的组件) - Django/React App: How to have both Django URLS (for the api to send data) and React URLS (to render my components) in my web app, compatibly 谷歌地图 JavaScript API 未显示在我的 React 应用程序中 - Google Maps JavaScript API not displaying in my React app 为什么我的 api 数据没有显示在 react.js 的页面上? - Why is my api data not displaying on the page for react.js? 如何在React App中保护我的API密钥? - How do I protect my API keys in my React App? 我的星座反应应用程序没有显示星座? - Horoscope not displaying from my Horoscope react app? 我的反应应用程序只是不显示图表 - My react app is just not displaying the graph 我的反应电子商务应用程序中的过滤器 function 有问题 - I have in issue with filter function in my react ecommerce app API Json 数据未在我的反应应用程序中由 map() 呈现 - API Json data not being rendered by map() in my react app 如何在React中对我的数据使用多个过滤器? - How can I have multiple filters on my data in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM