使用 React 构建食谱查找器网站

使用 react 构建食谱查找器网站

介绍

在本博客中,我们将使用 react 构建一个食谱查找网站。该应用程序允许用户搜索他们最喜欢的食谱,查看趋势或新食谱,并保存他们最喜欢的食谱。我们将利用 edamam api 获取实时食谱数据并将其动态显示在网站上。

项目概况

食谱查找器允许用户:

按名称搜索食谱。查看趋势和新添加的食谱。查看各个食谱的详细信息。将食谱添加到收藏夹列表并使用 localstorage 保存数据。

特征

搜索功能:用户可以通过输入查询来搜索食谱。热门食谱:显示来自 api 的当前热门食谱。新菜谱:显示来自 api 的最新菜谱。食谱详细信息:显示有关所选食谱的详细信息。收藏夹:允许用户将食谱添加到收藏夹列表,该列表保存在本地。

使用的技术

react:用于构建用户界面。react router:用于不同页面之间的导航。edamam api:用于获取食谱。css:用于设计应用程序的样式。

项目结构

src/│├── components/│   └── navbar.js│├── pages/│   ├── home.js│   ├── about.js│   ├── trending.js│   ├── newrecipe.js│   ├── recipedetail.js│   ├── contact.js│   └── favorites.js│├── app.js├── index.js├── app.css└── index.css

安装

要在本地运行此项目,请按照以下步骤操作:

克隆存储库:

   git clone https://github.com/abhishekgurjar-in/recipe-finder.git   cd recipe-finder

安装依赖项:

   npm install

启动 react 应用程序:

   npm start

从 edamam 网站获取您的 edamam api 凭证(api id 和 api 密钥)。

在进行 api 调用的页面中添加您的 api 凭据,例如 home.js、trending.js、newrecipe.js 和 recipedetail.js。

用法

应用程序.js
import react from "react";import navbar from "./components/navbar";import { route, routes } from "react-router-dom";import "./app.css";import home from "./pages/home";import about from "./pages/about";import trending from "./pages/trending";import newrecipe from "./pages/newrecipe";import recipedetail from "./pages/recipedetail";import contact from "./pages/contact";import favorites from "./pages/favorites";const app = () => {  return (                        <route path="/" element={} />        <route path="/trending" element={} />        <route path="/new-recipe" element={} />        <route path="/new-recipe" element={} />        <route path="/recipe/:id" element={} />        <route path="/about" element={} />        <route path="/contact" element={} />        <route path="/favorites" element={} />         

made with ❤️ by abhishek gurjar

主页.js

这是用户可以使用 edamam api 搜索食谱的主页。

import react, { usestate, useref, useeffect } from "react";import { iosearch } from "react-icons/io5";import { link } from "react-router-dom";const home = () => {  const [query, setquery] = usestate("");  const [recipe, setrecipe] = usestate([]);  const recipesectionref = useref(null);  const api_id = "2cbb7807";  const api_key = "17222f5be3577d4980d6ee3bb57e9f00";  const getrecipe = async () => {    if (!query) return; // add a check to ensure the query is not empty    const response = await fetch(      `https://api.edamam.com/search?q=${query}&app_id=${api_id}&app_key=${api_key}`    );    const data = await response.json();    setrecipe(data.hits);    console.log(data.hits);  };  // use useeffect to detect changes in the recipe state and scroll to the recipe section  useeffect(() => {    if (recipe.length > 0 && recipesectionref.current) {      recipesectionref.current.scrollintoview({ behavior: "smooth" });    }  }, [recipe]);  // handle key down event to trigger getrecipe on enter key press  const handlekeydown = (e) => {    if (e.key === "enter") {      getrecipe();    }  };  return (    

find your favourite recipe

setquery(e.target.value)} onkeydown={handlekeydown} // add the onkeydown event handler />
{recipe.map((item, index) => (
@@##@@

{item.recipe.label}

))}
);};export default home;
trending.js

此页面获取并显示趋势食谱。

import react, { usestate, useeffect } from "react";import { link } from "react-router-dom";const trending = () => {  const [trendingrecipes, settrendingrecipes] = usestate([]);  const [loading, setloading] = usestate(true);  const [error, seterror] = usestate(null);  const api_id = "2cbb7807";  const api_key = "17222f5be3577d4980d6ee3bb57e9f00";  useeffect(() => {    const fetchtrendingrecipes = async () => {      try {        const response = await fetch(          `https://api.edamam.com/api/recipes/v2?type=public&q=trending&app_id=${api_id}&app_key=${api_key}`        );        if (!response.ok) {          throw new error("network response was not ok");        }        const data = await response.json();        settrendingrecipes(data.hits);        setloading(false);      } catch (error) {        seterror("failed to fetch trending recipes");        setloading(false);      }    };    fetchtrendingrecipes();  }, []);  if (loading)    return (      
); if (error) return
{error}
; return (

trending recipes

{trendingrecipes.map((item, index) => (
@@##@@

{item.recipe.label}

))}
);};export default trending;
新菜谱.js

此页面获取新食谱并显示新食谱。

import react, { usestate, useeffect } from "react";import { link } from "react-router-dom";const newrecipe = () => {  const [newrecipes, setnewrecipes] = usestate([]);  const [loading, setloading] = usestate(true);  const [error, seterror] = usestate(null);  const api_id = "2cbb7807";  const api_key = "17222f5be3577d4980d6ee3bb57e9f00";  useeffect(() => {    const fetchnewrecipes = async () => {      try {        const response = await fetch(          `https://api.edamam.com/api/recipes/v2?type=public&q=new&app_id=${api_id}&app_key=${api_key}`        );        if (!response.ok) {          throw new error("network response was not ok");        }        const data = await response.json();        setnewrecipes(data.hits);        setloading(false);      } catch (error) {        seterror("failed to fetch new recipes");        setloading(false);      }    };    fetchnewrecipes();  }, []);  if (loading)    return (      
); if (error) return
{error}
; return (

new recipes

{newrecipes.map((item, index) => (
@@##@@

{item.recipe.label}

))}
);};export default newrecipe;
主页.js

此页面获取并显示主页和搜索的食谱。

import react, { usestate, useref, useeffect } from "react";import { iosearch } from "react-icons/io5";import { link } from "react-router-dom";const home = () => {  const [query, setquery] = usestate("");  const [recipe, setrecipe] = usestate([]);  const recipesectionref = useref(null);  const api_id = "2cbb7807";  const api_key = "17222f5be3577d4980d6ee3bb57e9f00";  const getrecipe = async () => {    if (!query) return; // add a check to ensure the query is not empty    const response = await fetch(      `https://api.edamam.com/search?q=${query}&app_id=${api_id}&app_key=${api_key}`    );    const data = await response.json();    setrecipe(data.hits);    console.log(data.hits);  };  // use useeffect to detect changes in the recipe state and scroll to the recipe section  useeffect(() => {    if (recipe.length > 0 && recipesectionref.current) {      recipesectionref.current.scrollintoview({ behavior: "smooth" });    }  }, [recipe]);  // handle key down event to trigger getrecipe on enter key press  const handlekeydown = (e) => {    if (e.key === "enter") {      getrecipe();    }  };  return (    

find your favourite recipe

setquery(e.target.value)} onkeydown={handlekeydown} // add the onkeydown event handler />
{recipe.map((item, index) => (
@@##@@

{item.recipe.label}

))}
);};export default home;
收藏夹.js

此页面显示最喜欢的食谱。

import react, { usestate, useeffect } from "react";import { link } from "react-router-dom";const favorites = () => {  const [favorites, setfavorites] = usestate([]);  useeffect(() => {    const savedfavorites = json.parse(localstorage.getitem("favorites")) || [];    setfavorites(savedfavorites);  }, []);  if (favorites.length === 0) {    return 
no favorite recipes found.
; } return (

favorite recipes

    {favorites.map((recipe) => (
    @@##@@

    {recipe.label}

    ))}
);};export default favorites;
recipedetail.js

此页面显示食谱。

import react, { usestate, useeffect } from "react";import { useparams } from "react-router-dom";const recipedetail = () => {  const { id } = useparams(); // use react router to get the recipe id from the url  const [recipe, setrecipe] = usestate(null);  const [loading, setloading] = usestate(true);  const [error, seterror] = usestate(null);  const [favorites, setfavorites] = usestate([]);  const api_id = "2cbb7807";  const api_key = "17222f5be3577d4980d6ee3bb57e9f00";  useeffect(() => {    const fetchrecipedetail = async () => {      try {        const response = await fetch(          `https://api.edamam.com/api/recipes/v2/${id}?type=public&app_id=${api_id}&app_key=${api_key}`        );        if (!response.ok) {          throw new error("network response was not ok");        }        const data = await response.json();        setrecipe(data.recipe);        setloading(false);      } catch (error) {        seterror("failed to fetch recipe details");        setloading(false);      }    };    fetchrecipedetail();  }, [id]);  useeffect(() => {    const savedfavorites = json.parse(localstorage.getitem("favorites")) || [];    setfavorites(savedfavorites);  }, []);  const addtofavorites = () => {    const updatedfavorites = [...favorites, recipe];    setfavorites(updatedfavorites);    localstorage.setitem("favorites", json.stringify(updatedfavorites));  };  const removefromfavorites = () => {    const updatedfavorites = favorites.filter(      (fav) => fav.uri !== recipe.uri    );    setfavorites(updatedfavorites);    localstorage.setitem("favorites", json.stringify(updatedfavorites));  };  const isfavorite = favorites.some((fav) => fav.uri === recipe?.uri);  if (loading)    return (      
); if (error) return
{error}
; return (
{recipe && (

{recipe.label}

ingredients:

    {recipe.ingredientlines.map((ingredient, index) => (
  • {ingredient}
  • ))}

instructions:

{/* note: edamam api doesn't provide instructions directly. you might need to link to the original recipe url */}

for detailed instructions, please visit the{" "} recipe instruction

{isfavorite ? ( ) : ( )}
@@##@@
);};export default recipedetail;
联系方式.js

此页面显示联系页面。

import react, { usestate } from 'react';const contact = () => {  const [name, setname] = usestate('');  const [email, setemail] = usestate('');  const [message, setmessage] = usestate('');  const [showpopup, setshowpopup] = usestate(false);  const handlesubmit = (e) => {    e.preventdefault();    // prepare the contact details object    const contactdetails = { name, email, message };    // save contact details to local storage    const savedcontacts = json.parse(localstorage.getitem('contacts')) || [];    savedcontacts.push(contactdetails);    localstorage.setitem('contacts', json.stringify(savedcontacts));    // log the form data    console.log('form submitted:', contactdetails);    // clear form fields    setname('');    setemail('');    setmessage('');    // show popup    setshowpopup(true);  };  const closepopup = () => {    setshowpopup(false);  };  return (    

contact us

setname(e.target.value)} required />
setemail(e.target.value)} required />
{showpopup && (

thank you!

your message has been submitted successfully.

)}
);};export default contact;
关于.js

此页面显示关于页面。

import React from 'react';const About = () => {  return (    

About Us

Welcome to Recipe Finder, your go-to place for discovering delicious recipes from around the world!

Our platform allows you to search for recipes based on your ingredients or dietary preferences. Whether you're looking for a quick meal, a healthy option, or a dish to impress your friends, we have something for everyone.

We use the Edamam API to provide you with a vast database of recipes. You can easily find new recipes, view detailed instructions, and explore new culinary ideas.

Features:

  • Search for recipes by ingredient, cuisine, or dietary restriction.
  • Browse new and trending recipes.
  • View detailed recipe instructions and ingredient lists.
  • Save your favorite recipes for quick access.

Our mission is to make cooking enjoyable and accessible. We believe that everyone should have the tools to cook great meals at home.

);};export default About;

现场演示

您可以在这里查看该项目的现场演示。

结论

食谱查找网站对于任何想要发现新的和流行食谱的人来说是一个强大的工具。通过利用 react 作为前端和 edamam api 来处理数据,我们可以提供无缝的用户体验。您可以通过添加分页、用户身份验证甚至更详细的过滤选项等功能来进一步自定义此项目。

随意尝试该项目并使其成为您自己的!

制作人员

api:毛豆图标:react 图标

作者

abhishek gurjar 是一位专注的 web 开发人员,热衷于创建实用且功能性的 web 应用程序。在 github 上查看他的更多项目。

{item.recipe.label}{item.recipe.label}{item.recipe.label}{item.recipe.label}{recipe.label}{recipe.label}

以上就是使用 React 构建食谱查找器网站的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1629873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月24日 12:49:26
下一篇 2025年12月24日 12:49:31

相关推荐

  • 使用 React 构建国家/地区查找应用程序

    介绍 在这篇博文中,我们将探索如何使用 react 构建国家/地区查找应用程序。该应用程序允许用户搜索国家/地区、按地区过滤它们以及查看有关每个国家/地区的详细信息。我们将利用 react 的钩子和上下文来管理状态和主题,并将与 rest 国家/地区 api 集成以获取国家/地区数据。 项目概况 国…

    2025年12月24日 好文分享
    000
  • 使用 React 构建加密货币查找器应用程序

    介绍 加密货币如今风靡一时,随着可用硬币的数量过多,有一个工具可以轻松搜索和查看它们的详细信息是至关重要的。 crypto finder 应用程序就是这样做的。该应用程序使用 react 构建,为用户搜索、过滤和查看加密货币详细信息提供无缝体验。 项目概况 crypto finder 应用程序包括:…

    2025年12月24日 好文分享
    000
  • CSS 边框 – 设计元素的轮廓

    这是您的css:从基础到辉煌系列的下一篇文章: 第 9 讲:css 边框 – 设置元素轮廓的样式 在本次讲座中,我们将探讨如何使用 css 添加和自定义 html 元素周围的边框。边框可以显着影响元素的视觉外观并定义网页的各个部分。 1.基本边框属性 css 边框是使用三个关键属性定义的: bord…

    2025年12月24日
    000
  • 魅力我的标记:太阳系

    这是前端挑战 v24.09.04 的提交,Glam Up My Markup: Space 我建造了什么 这些响应式太阳系网站试图实现令人惊叹的视觉效果、互动性和教育性,并且如挑战标准中所述,易于访问且易于使用。 演示 项目 GitHub 链接 项目部署链接 太阳系 旅行 由于模板不包含任何设计元素…

    2025年12月24日
    000
  • CSS 边距 – 元素周围的间距

    这是您的css:从基础到辉煌系列的下一篇文章: 第 10 讲:css 边距 – 元素周围的间距 在本次讲座中,我们将深入研究 css 边距,它控制 html 元素周围的空间。页边距在确定网页上元素的布局和位置方面发挥着至关重要的作用,确保元素不重叠并具有适当的间距。 1.什么是边距? 边距定义元素边…

    2025年12月24日
    000
  • Monkeytype 反应克隆 ⌨️

    我很高兴分享我一直在从事的一个项目——用 React 构建的流行打字网站 Monkeytype 的克隆。我已将代码向社区公开,特别是对于那些对如何使用此框架开发此类项目感到好奇的人。虽然我并不是说这是构建它的最佳方法,但我发现这是有效的方法,我希望它可以成为对其他人有用的资源。我很乐意与大家分享并听…

    2025年12月24日
    000
  • 高级 CSS 网格技术

    第 10 讲:高级 css 网格技术 欢迎来到《从基础到辉煌》课程第十讲。在本次讲座中,我们将深入研究高级 css 网格技术。这些技术将使您能够创建更复杂和响应更快的布局。在本讲座结束时,您将能够使用网格区域、网格自动放置,并将 css 网格与 flexbox 等其他布局系统结合起来。 1.网格区域…

    2025年12月24日
    000
  • Miracle UI – React 组件库

    大家好,我想向大家介绍我的项目 Miracle UI,一个完全用 CSS 构建的组件库。这使得该库超级轻量且易于使用。许多组件都带有大量属性,因此您可以以您想象不到的方式自定义它们。我邀请您查看 npm 包,亲眼看看每个组件到底有多轻量。 话虽如此,我想澄清一下,我是一名学生,我自己开发了这个项目,…

    2025年12月24日
    000
  • CSS 网格:嵌套网格布局

    介绍 css grid 是一种布局系统,因其在创建多列布局方面的灵活性和效率而迅速受到 web 开发人员的欢迎。它最有用的功能之一是能够创建嵌套网格布局。嵌套网格可以在设计复杂网页时提供更多控制和精确度。在本文中,我们将探讨在 css 网格中使用嵌套网格布局的优点、缺点和主要功能。 优点 嵌套网格布…

    2025年12月24日
    000
  • Tailwind CSS:优化性能

    介绍 tailwind css 是一种流行的基于实用程序的 css 框架,可帮助开发人员高效地创建现代且直观的用户界面。 tailwind css 背后的主要原则之一是专注于性能优化。在本文中,我们将探讨 tailwind css 在性能方面的优缺点,并仔细研究其主要功能。 优点 tailwind …

    2025年12月24日
    000
  • 使用 React 构建二维码生成器

    介绍 在本教程中,我们将使用 react 创建一个 qr 代码生成器 web 应用程序。对于那些希望了解集成 api、管理状态和生成动态内容的人来说,该项目是理想的选择。 项目概况 二维码生成器允许用户通过输入内容、调整大小和选择背景颜色来创建二维码。它利用公共 api 生成 qr 码并将其显示在屏…

    2025年12月24日
    000
  • 使用 React 构建歌词查找器应用程序

    介绍 在本教程中,我们将使用 react 创建一个 lyrics finder web 应用程序。该项目非常适合那些想要练习集成 api、管理状态和显示动态内容的人。 项目概况 歌词查找器允许用户通过输入歌曲标题和艺术家姓名来搜索歌词。它从公共 api 获取歌词并将其显示在屏幕上。用户可以快速找到并…

    2025年12月24日 好文分享
    000
  • CSS 定位 – 绝对、相对、固定和粘性

    第 11 讲:css 定位 – 绝对、相对、固定和粘性 欢迎来到《从基础到辉煌》课程第十一讲。在本次讲座中,我们将探讨css定位的不同类型:相对、绝对、固定和粘性。了解定位可以让您控制元素在页面上的显示位置以及用户与内容交互时元素的行为方式。 1.了解位置属性 position 属性指定元素在文档中…

    2025年12月24日
    000
  • Tailwind CSS 与 Vanilla CSS:何时在 Web 开发项目中使用每种 CSS

    构建网站或 Web 应用程序时,使用 Tailwind CSS 和 vanilla CSS 之间的决定可能会显着影响您的工作流程、设计一致性和项目可扩展性。这两种选择都具有独特的优势,但正确的选择取决于您的具体项目要求和目标。 在本文中,我们将深入探讨 Tailwind CSS 和 vanilla …

    2025年12月24日
    000
  • 使用 React 构建主题切换的 Todo 应用程序

    介绍 在本教程中,我们将使用 react 构建一个 待办事项列表 web 应用程序。该项目有助于理解状态管理、事件处理以及在 react 中使用列表。对于想要增强 react 开发技能的初学者来说,它是完美的选择。 项目概况 待办事项列表应用程序允许用户添加、标记为已完成和删除任务。它提供了一个干净…

    2025年12月24日 好文分享
    000
  • 构建 React 费用跟踪应用程序

    介绍 在本教程中,我们将使用 react 创建一个 expense tracker web 应用程序。该项目将帮助您了解 react 中的状态管理、事件处理和动态列表更新。对于旨在通过构建实用且有用的应用程序来加强 react 开发知识的初学者来说,它是理想的选择。 项目概况 费用跟踪应用程序允许用…

    2025年12月24日 好文分享
    000
  • 月相 | CSS 艺术:空间

    CSS 艺术:互动空间场景 这是前端挑战 v24.09.04,CSS 艺术:空间的提交。 灵感 对于这个挑战,我想捕捉夜空的动态和互动性质。不断变化的月相、闪烁的星星,以及偶尔令人兴奋的流星,一直让人类着迷。通过创建这些天体现象的动画和交互式表示,我的目标是将宇宙的一小部分带到我们的屏幕上,提醒我们…

    2025年12月24日
    000
  • 使用 Tailwind CSS 创建流星边框动画

    在这篇博文中,我们将使用 tailwind css 创建一个迷人的“流星”边框动画。此效果为输入字段提供发光的动画边框,可以吸引用户的注意力 – 非常适合电子邮件注册或重要通知等号召性用语部分。 演示 在深入研究代码之前,您可以在此处查看效果的现场演示:在 tailwind playgr…

    2025年12月24日
    000
  • HTML、CSS 和 JavaScript 项目

    欢迎来到我的 html、css 和 javascript 项目集合!这篇博文全面概述了我创建的各种项目,展示了 web 开发的不同方面。每个项目都可以在自己的存储库中找到,其中包含您需要探索和学习的所有代码。 目录 简介项目概况开始使用贡献作者 介绍 作为一名 web 开发人员,我喜欢从事各种项目,…

    2025年12月24日
    000
  • Riva – Tailwind CSS 仪表板模板生成器

    大家好! 我想向您介绍 Riva Dashboard,这是一个用于 Tailwind CSS 的拖放式仪表板模板生成器,旨在帮助开发人员加快开发过程。 Riva 构建于 Tailwind CSS 之上,具有以下功能,包含 72 多个组件(更多组件即将推出)。 链接: 立即学习“前端免费学习笔记(深入…

    2025年12月24日
    000

发表回复

登录后才能评论
关注微信