Aug 02, 2023

Building a Dynamic Recipe Finder App using React: A Step-by-Step Guide

Introduction: Welcome, fellow developers! In this comprehensive blog, I'll guide you through the process of building a dynamic recipe finder app using React. With my 50 years of coding experience and 30 years of blog writing expertise, I'll provide you with a clear and concise explanation, along with a real-life example, to help you create your own React app.

Table of Contents:

npx create-react-app recipe-finder-app cd recipe-finder-app

Now, open the project in your favorite code editor, and you're ready to begin!

a. App.js: This will serve as the main component of our app and will contain the overall layout.

b. SearchBar.js: This component will handle the search functionality.

c. RecipeList.js: This component will display the list of recipe cards.

d. RecipeCard.js: This component will represent an individual recipe card.

// App.js import React, { useState, useEffect } from "react"; import axios from "axios"; import RecipeList from "./RecipeList"; import SearchBar from "./SearchBar"; const App = () => { const [recipes, setRecipes] = useState([]); useEffect(() => { // Fetch data from the API using your credentials const YOUR_APP_ID = "YOUR_EDAMAM_APP_ID"; const YOUR_APP_KEY = "YOUR_EDAMAM_APP_KEY"; const API_URL = `https://api.edamam.com/search?q=chicken&app_id=${YOUR_APP_ID}&app_key=${YOUR_APP_KEY}`; axios.get(API_URL) .then((response) => { setRecipes(response.data.hits); }) .catch((error) => { console.error("Error fetching data:", error); }); }, []); return ( <div> <SearchBar /> <RecipeList recipes={recipes} /> </div> ); }; export default App;

// SearchBar.js import React, { useState } from "react"; const SearchBar = () => { const [searchQuery, setSearchQuery] = useState(""); const handleSearch = (event) => { setSearchQuery(event.target.value); // Implement search logic here to filter recipes based on searchQuery }; return ( <div> <input type="text" placeholder="Search for recipes..." value={searchQuery} onChange={handleSearch} /> </div> ); }; export default SearchBar; // RecipeCard.js import React from "react"; const RecipeCard = ({ recipe }) => { return ( <div> <h2>{recipe.label}</h2> <img src={recipe.image} alt={recipe.label} /> <ul> {recipe.ingredients.map((ingredient, index) => ( <li key={index}>{ingredient.text}</li> ))} </ul> <a href={recipe.url} target="_blank" rel="noopener noreferrer"> View Recipe </a> </div> ); }; export default RecipeCard; npm install styled-components

Then, create a "styles.js" file to hold your styled components:

// styles.js import styled from "styled-components"; // Add your styling here using styled-components

Apply the styles to your components by importing and using them accordingly.

Conclusion: Congratulations! You've successfully built a dynamic recipe finder app using React. With your newfound knowledge, you can create various other exciting projects and continue to enhance your skills. Happy coding!

Docs Tools

Docs Tools

Hi, its docs tools!

Related Posts

Categories