Building a Complete Food Mobile App with React Native and Strapi

In this tutorial, we will guide you through the process of creating a food mobile app using React Native and Strapi. the app will enable users to browse a list of available foods and add them to their cart.

Strapi
Strapi
13 min readNov 28, 2023

--

In most advanced cities, people no longer need to physically visit restaurants to order the food they desire. The internet enables them to order food online and have it delivered promptly to their preferred location, whether it’s their office or home. With the food ordering app, you can browse through the list of available food menus, place an order, and have it delivered to you without leaving your comfort zone.

This tutorial will walk you through building a food ordering mobile app using React Native and Strapi. The app will feature functionalities for users to browse through a selection of foods, add them to their cart, and manage user authentication and profiles. By the end of this guide, you will have created a mobile food application similar to the one shown in the following image:

Application preview

Prerequisites

Before diving into this tutorial, ensure you have the following prerequisites:

Setting up Strapi

Let’s set up the Strapi backend for the application. If you already have a Strapi project, you can skip to the next section. Otherwise, open your terminal and create a new Strapi project using the command below.

npx create-strapi-app@latest my-project --quickstart

After the installation is complete, the Strapi server will start and open in your browser. Register and log in to your dashboard as shown in the image below.

Creating Strapi Collection Types

Next, we’ll create collection types for our application, specifically: RegisterUsers, Product, and Cart. Here's how to set them up:

Step 1: Create RegisterUsers Collection type

  1. In the Strapi dashboard, go to the Content-Type Builder in the side menu.
  2. Click on Create New Collection Type.
  3. Name the collection type RegisterUsers and click Continue``. Refer to the image below for guidance.

Step 2: Structure the RegisterUsers Collection

Now let’s structure the registerusers collection type by adding the following fields to it:

  • fullname: type→text
  • email: type→Email
  • password: type→text
  • city: type→text
  • state: type→text

Creating Additional Collection Types

Repeat the above steps to create the following collection types:

Product Collection:

  • productname: type→text
  • price: type→text
  • image: type→media

Cart Collection: Create a Cart collection type containing the following fields.

  • productid: type→text
  • userid: type→text

After creating the collection types, navigate to Settings → Role → Public and select all operations for each collection type to make them accessible via the API endpoint, as shown in the image below.

Now, navigate to the content manager, click on the “Product" collection type, and add as many products as you'd like.

React Native Installation and Setup

With the Strapi backend ready, our next step is to build the mobile application interface. The complete source code for this mobile app is available on GitHub.

To get started, let’s clone the mobile application to your local computer. Open your terminal and execute the following command:

git clone https://github.com/popoolatopzy/StrapiFoodApp

After cloning the application, let’s install all the necessary dependencies. To install the dependencies run the commands below on your terminal. cd StrapiFoodApp npm install

The above command installs important dependencies like: React Navigation: This is a popular and widely used library for managing navigation and routing in React Native applications. It provides a structured approach to handling the app’s navigation flow, encompassing features such as screen creation, stack navigation, tab navigation, and drawer navigation. React Native Async Storage: A package that provides a straightforward, asynchronous key-value storage system. It is used for caching data, saving user preferences, and restoring session information.

Creating the Application Screens

Open the cloned app in your code editor. The app includes four primary screens located in the “components` folder. We’ll explore these components for a better understanding of their functionality.

Authentication Screens Before users can access the food app, they must first register and log in to the application. To create the registration component, navigate to the ‘components' folder, create a ‘RegisterScreen.js' file, and add the following code.

From the code above, we created a registration form and then sent the user details to our Strapi “registeruser" endpoint to store the user information. After successful registration, the user is redirected to the login screen.

Next, create a LoginScreen.js file inside the component folder. This component is used by the registered user to log into their account using an email and password. Add the following code the file.

From the code above, we created a login form and checked if the user email and password is correct by making a request to the registeruser endpoint to fetch the user information. After successful registration, the user is redirected to the home screen.

Food home screen All the lists of available products are displayed in the home screen, where users can browse through products, view them and add products to cart. All the products are fetched from the Strapi product endpoint.

Create a HomeScreenn.js file and add the following code into it:

From the code above, we make a GET request to the Strapi product endpoint to retrieve and display the list of all available food items. Users can click on a product to view more details about the food.

Create the Product Preview screen

Now, we will create a Product Preview screen that shows more details about the food the user wants to order, and they can add the food to their cart list. In the components folder, create ViewScreen and add the following code to it.

In the code above, we retrieved the product ID of the preview food from the screen URL using React Router and made a request to the Strapi product endpoint to fetch details about the product, passing the product ID along with the request.

Also, the handleAddToCart() function is used to add a new product to the user's cart list by making a POST request to the cart endpoint.

Create the cart screen

Now, let’s create a Cart Screen where all the foods the users have added to the cart will be displayed. To do that, create a ‘CartScreen.js' file in the 'component' folder and add the following code to it.

In the code above, we fetch the list of all the food that a user has added to the cart and then calculate and display the total price for all the food in the cart.

Create the profile screen

To create a profile screen where details about the logged-in user are displayed with a logout button, inside the component folder, create a ProfileScreen.js file and add the following code to it.

In the code above, user information is retrieved from local storage and displayed on the profile screen, while the ‘log out' button ends the user session and redirects them to the login screen.

Wrapping Up

Now that we have created all the necessary components, let’s connect all the components together using React Native Navigation. Open the 'app.js' file from the project's root folder, and add the following code to it:

Next, let’s start the application by running the command below on your terminal. npx expo start

After starting the application server, follow the prompts to get the application running on your simulator. The application should be up and running, as shown in the image below.

Congratulations, you have successfully created a mobile food application using React Native and Strapi!

Conclusion

Throughout this tutorial, we have successfully walked through the process of creating a food ordering application utilizing React Native and Strapi. For further reference and exploration:

Feel free to dive into these resources for a deeper understanding and to customize your application.

--

--

Strapi
Strapi

The open source Headless CMS Front-End Developers love.