- Lab
- 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.

Path 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
RecipeList
component. 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.js
file, it's time to define theRecipeList
component within it. You'll start by makingRecipeList
accept two props:recipes
andselectRecipe
. The recipes prop will be an array of recipe objects that you'll be displaying in the list. TheselectRecipe
prop is a function intended for setting the currently viewed recipe. With theRecipeList
component in place, the next step is to dynamically render the list ofrecipes
. You'll achieve this by iterating over therecipes
prop using themap
function. 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 anonClick
event handler to each recipe's<div>
, you enable users to interact with the list. This handler will call theselectRecipe
function, 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 theRecipeList
component, it's important to prepare theApp
component with the necessary state. This involves using React'suseState
hook 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 howRecipeList
renders this data. With the state set up, the next step is to import theRecipeList
component 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 theRecipeList
component withinApp.js
. By passing therecipes
state andselectRecipe
function 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
RecipeDetails
component to show detailed information about a selected recipe, including its name, ingredients, and instructions. You'll first create theRecipeDetails
component and then updateApp.js
to manage state and conditionally render the recipe details based on user selection from theRecipeList
. Now, you'll focus on enhancing theRecipeDetails
component 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.js
to include a state variable that keeps track of the currently selected recipe. Additionally, you'll modify theselectRecipe
function 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
RecipeDetails
component only when there is a recipe currently selected, i.e., when the recipe state variable is notnull
. This dynamic rendering approach ensures that theRecipeDetails
component 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
recipe
state inApp.js
. Based on its value (whether it's null or holds a recipe object), theRecipeDetails
component will be rendered accordingly. You can do this by using the&&
operator, e.g:conditionThatMustBeTrue && <Component />
This would only render
Component
ifconditionThatMustBeTrue
was 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 theRecipeDetails
component. -
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
RecipeForm
component, managing form state, and handling a form submission to update the main application state with the new recipe. Now that theRecipeForm
component 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 thevalue
attribute and set uponChange
handlers. 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 theonSubmit
handler 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 ahandleSubmit
function 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
, andinstructions
to 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
handleSubmit
function to the form'sonSubmit
event is crucial. This linkage ensures that when the form is submitted, your customhandleSubmit
function 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 theRecipeForm
into your application, allowing for the addition of new recipes. When you update the recipes state in theaddRecipe
function, 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 passingaddRecipe
as 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
RecipeForm
component that you've been building, render it, and pass it the necessary props so that it can update the state inApp.js
Congratulations 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.css
inRecipeList.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!
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.