- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Build a Recipe Sharing App in React
In this Guided Code Lab, you will build a Recipe Sharing App using React, where you'll learn to set up dynamic components, manage state and forms, and create interactive features. This hands-on approach is designed to reinforce fundamental React concepts, making it ideal for both beginners and those looking to deepen their web development skills.
Lab Info
Table of Contents
-
Challenge
Introduction and Environment Setup
In this React guided lab, you'll enhance a starter app into a recipe sharing platform for Globomantics. The guided steps in this lab will cover React fundamentals such as component creation, state management, props handling, and form interactions.
You can validate your tasks by clicking the checkboxes next to them. If you need help or want to compare solutions, a solution directory is provided. You can launch the app at any time during the lab by doing the following:
- Click the Run button at the bottom right of the Terminal.
- View the web page by clicking this link: {{localhost:3000}}.
- You can also refresh the Web Browser tab to view the page once the app is running. It may take a moment for the page to load.
-
Challenge
Building the Recipe List Component
In this part of the lab, you'll be building a core piece of the app - the
RecipeListcomponent. This component will be responsible for displaying a list of recipes. You will also integrate this component into the main application (App.js). Now that you have created theRecipeList.jsfile, it's time to define theRecipeListcomponent within it. You'll start by makingRecipeListaccept two props:recipesandselectRecipe. The recipes prop will be an array of recipe objects that you'll be displaying in the list. TheselectRecipeprop is a function intended for setting the currently viewed recipe. With theRecipeListcomponent in place, the next step is to dynamically render the list ofrecipes. You'll achieve this by iterating over therecipesprop using themapfunction. This approach not only renders the recipe names, but also sets the stage for further interactivity, such as selecting a recipe to view more details. After rendering the recipe names, the next step is to make these items interactive. By adding anonClickevent handler to each recipe's<div>, you enable users to interact with the list. This handler will call theselectRecipefunction, passing the clicked recipe's data. This functionality is foundational for allowing users to select a recipe and view more details about it. Before integrating theRecipeListcomponent, it's important to prepare theAppcomponent with the necessary state. This involves using React'suseStatehook to create a state variable that will store your recipes. Initializing this state with a couple of example recipes will later allow you to see howRecipeListrenders this data. With the state set up, the next step is to import theRecipeListcomponent intoApp.js. Additionally, you'll define a placeholder function namedselectRecipe. This function will be crucial for handling user interactions with the recipe list, but its detailed implementation will come later. Now that you have your state and functions ready, it's time to render theRecipeListcomponent withinApp.js. By passing therecipesstate andselectRecipefunction as props, you're connecting App's state toRecipeList, enabling it to display the list of recipes and respond to user actions. -
Challenge
Creating a Recipe Details Component
In this step, you'll develop the
RecipeDetailscomponent to show detailed information about a selected recipe, including its name, ingredients, and instructions. You'll first create theRecipeDetailscomponent and then updateApp.jsto manage state and conditionally render the recipe details based on user selection from theRecipeList. Now, you'll focus on enhancing theRecipeDetailscomponent to display a recipe's name, ingredients, and instructions. This task is aimed at presenting these key details clearly, making the recipe user-friendly. In the next task, you'll updateApp.jsto include a state variable that keeps track of the currently selected recipe. Additionally, you'll modify theselectRecipefunction to update this state, effectively managing which recipe is selected at any given time. This addition is key to dynamically displaying recipe details based on user interactions. Next, you'll implement conditional rendering within your React application. Conditional rendering in React is a powerful technique used to display components or elements based on certain conditions. In this specific task, the condition revolves around whether a recipe is selected or not.The core idea here is to render the
RecipeDetailscomponent only when there is a recipe currently selected, i.e., when the recipe state variable is notnull. This dynamic rendering approach ensures that theRecipeDetailscomponent and its content are only shown to the user when relevant, enhancing the application's interactivity and user experience.To achieve this, you will utilize the existing
recipestate inApp.js. Based on its value (whether it's null or holds a recipe object), theRecipeDetailscomponent will be rendered accordingly. You can do this by using the&&operator, e.g:conditionThatMustBeTrue && <Component />This would only render
ComponentifconditionThatMustBeTruewas a truthy value. It's time is to test your application's functionality! Ensure that selecting a recipe from the list updates the displayed details in theRecipeDetailscomponent. -
Challenge
Adding a New Recipe with a Form
In this step, you'll be enhancing the functionality of your Recipe Sharing App by adding a form that allows users to submit new recipes. This involves creating a
RecipeFormcomponent, managing form state, and handling a form submission to update the main application state with the new recipe. Now that theRecipeFormcomponent has been created, the next step is to enhance its functionality by adding form elements. This involves integrating input fields for the recipe's name, ingredients, and instructions. These additions are key for enabling users to input and submit their own recipes, making the form an interactive and essential feature of your application. Now that you have your form component, the next step is to create its internal state. This will allow you to track the user's input as they fill out the form. Now, you will bind each input field to its corresponding state variable using thevalueattribute and set uponChangehandlers. These handlers will update the state variables whenever the user types into the fields, ensuring the component reacts to user input. To enable users to submit their recipes, you will need to add a submit button to your form. Hitting this button is what will allow you to kick off theonSubmithandler you will create in later tasks. In the last step you created and implemented a submit button, but currently it doesn't do much. Let's create ahandleSubmitfunction that will allow you to handle the form submissions, using custom logic that you implement. The focus now is on finalizing the form submission process. After submitting the recipe data, it's important to clear the form fields to allow for new entries. This is achieved by resetting the state variablesname,ingredients, andinstructionsto empty strings. Resetting these fields ensures a clean state for the next submission, enhancing user experience by preventing the need to manually clear the form after each entry.Furthermore, attaching the
handleSubmitfunction to the form'sonSubmitevent is crucial. This linkage ensures that when the form is submitted, your customhandleSubmitfunction is triggered instead of the default form submission behavior. It allows the application to handle the form data according to the defined logic inhandleSubmit, which includes both processing the current input and preparing the form for subsequent entries. This step encapsulates the core functionality of form handling in React, combining state management with event handling for effective and responsive user interactions. Next, you'll integrate theRecipeForminto your application, allowing for the addition of new recipes. When you update the recipes state in theaddRecipefunction, you'll use the spread operator. This operator is key in React's state management, as it enables you to create a new array by spreading out the elements of the existing recipes array and then adding the new recipe to it. This approach ensures that your state updates immutably, maintaining React's state consistency and reactivity. By passingaddRecipeas a prop toRecipeForm, you enable the form to submit new recipes, which then get displayed in your recipe list.You're almost there! As a final task, you need to import the
RecipeFormcomponent that you've been building, render it, and pass it the necessary props so that it can update the state inApp.jsCongratulations on successfully building a basic recipe sharing app for Globomantics! You've mastered the intricacies of React, from setting up components to managing state and handling form submissions. To enhance your app's appearance, you can create a corresponding CSS file for each component insrc/components/styles. Import these CSS files into their respective components (e.g., importRecipeList.cssinRecipeList.js) to customize the look and feel of your app.This application is a great foundation for building more complex applications. Keep experimenting and refining your skills. Happy coding!
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.