简体   繁体   English

我的 Api 数据未显示在我的 React 应用程序上

[英]My Api data is not displaying on my react app

Im making a project where I fetch an image of a recipe card from https://spoonacular.com and I want it displayed on my react.js app.我正在制作一个项目,我从https://spoonacular.com获取食谱卡的图像,我希望它显示在我的 react.js 应用程序上。 For some reason I can't get the API data from displaying on the page when I run it.出于某种原因,当我运行它时,我无法在页面上显示 API 数据。 Please help Im really stuck请帮助我真的卡住了

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/4632/card?apiKey=${APIKey}`;

function Home() {
  const [food, setFood] = useState();

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

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

export default Home;

and this is my Recipe.js这是我的 Recipe.js

import React from "react";

function Recipe({ recipeList }) {
  return (
    <section className="RecipeCard">
      <div
        className="Recipe"
        dangerouslySetInnerHTML={{ __html: recipeList }}
      />
    </section>
  );
}

export default Recipe;

You need to output the JSON as HTML for it to display.您需要将 JSON 输出为 HTML 才能显示。

For example, get a specific recipe : https://api.spoonacular.com/recipes/716429/information?includeNutrition=false例如,获取特定食谱https : //api.spoonacular.com/recipes/716429/information?includeNutrition=false

and in the Recipe Component div并在Recipe组件div

<div className="Recipe">
    <div>{recipeList.title}</div>
    <img src={recipeList.image} />
</div>
 <section className="RecipeCard">
  {recipeList.map((recipe, index)=> (
    <div key={index} className="Recipe">
        <h2>{recipe.title}</h2>
   </div>
  ))}
</section>

Also, for code readability try to use async-await syntax for API call.此外,为了代码可读性,尝试对 API 调用使用 async-await 语法。

useEffect(()=> {
    const getRecipeList = async () => {
       if(food){
         try{
           const { data } = await axios.get(URL);
           setFood(data);
         }catch(error){
           console.log(error);
         }
       }
   }
  getRecipeList(); 
},[food]);

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

相关问题 我在 React 应用程序上显示我的 api 数据时遇到问题 - I have trouble displaying my api data on my react app 谷歌地图 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? 我的星座反应应用程序没有显示星座? - Horoscope not displaying from my Horoscope react app? 我的反应应用程序只是不显示图表 - My react app is just not displaying the graph API Json 数据未在我的反应应用程序中由 map() 呈现 - API Json data not being rendered by map() in my react app 创建我的第一个反应应用程序并且我的 fontawesome 图标没有显示或加载? - creating my first react app and my fontawesome icon is not displaying or loading? 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 在反应应用程序中运行 npm 启动后,我的浏览器上没有显示任何内容 - Nothing is Displaying on my browser after running npm start in react app 如何在React App中保护我的API密钥? - How do I protect my API keys in my React App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM