Build Real-World React Native App #7: Send Feedback with Formik, Yup, Firebase Cloud Function and Sendgrid

Build Real-World React Native App #7: Send Feedback with Formik ,Yup ,Firebase Cloud Function and Sendgrid

Build Real-World React Native App #7: Send Feedback with Formik, Yup, Firebase Cloud Function, and Sendgrid

In this chapter, we will create a simple form in the Feedback.js file using Formik and submit form data to the Firebase Realtime Database. Then, we will subsequently forward the message to the sender’s email using Cloud Function and Sendgrid. The form will be for users to send their feedback on the post articles and the app.

Let’s get started!

Setup Formik and Yup

First, we are going to install the required packages Formik and Yup. Formik package enables us to build forms whereas the Yup package is for form validation. We are also going to install another package that is react-native-keyboard-aware-scroll-view which enables us to scroll the view upwards when the keyboard pops up from the bottom. This will provide a better user experience. Now, in order to install these packages, we need to run the command given in the following code snippet in our project terminal:

Now, we need to open the Feedback.js file and import the necessary packages and their components as shown in the code snippet below:

Next, we need to create a simple form using Formik. First, we need to set the initial input values as blank. When a user submits the form, we need to call the submitForm function which will also display an error if any using helper text. The function that returns the form configuration is provided in the code snippet below:

Next, we need to create the validation rules using Yup as shown in the code snippet below:

Hence, we will get the form view as shown in the emulator screenshots below:

Feedback screen with React Native

Feedback screen with React Native

And, when we submit the form with empty fields, we will get the error message as well as shown in the emulator screenshots below:

Yup validation in React Native

Yup validation in React Native

Now that we have the form view, we will move on to set up Firebase in our React Native project.

Setting up React Native Firebase

Here, we are going to use the react-native firebase version 6 package in order to access Firebase services. Initially, we only require the core Firebase package and real-time database. So, we need to install them using the command provided below:

Setup on iOS

In iOS, we start by installing React native Firebase on cacao pod by using the command provided below:

Next, we need to open the project with Xcode and find the Bundle identifier as shown in the screenshot below:

Xcode setup

Xcode setup

Next, we need to go to Firebase Console and create a new app. Then, choose an iOS app and add Bundle identifier as shown in the example screenshots below:

Add new Firebase app

Add new Firebase app

Next, we need to download the GoogleService-info.plist file as shown in the screenshot below:

Download Google service info

Download Google service info

Then, we need to move the plist file to the Xcode project structure as shown in the screenshot below:

Add Google service info to Xcode

Add Google service info to Xcode

Next, we need to open Appdelegate.m file in Xcode and import Firebase then activate [FIRApp configure]; as highlighted in the example screenshot below:

Xcode appdelegate

Xcode appdelegate

Now, if everything works according to the right setup then we will see the following status on Firebase when we re-run the app:

Finalise setup IOS in Firebase

Finalise setup IOS in Firebase

Setup on Android

For Android, we need to locate the file named MainApplication.java as shown in the screenshot below:

Mainapplication.java

Mainapplication.java

Then, we need to copy the package name back to the Firebase console. The package name that we will get in the MainApllication.java file is shown below:

After copying, we need to create a new android app in the Firebase console and paste the package name in the config form as shown in the screenshot below:

Add Firebase to Android app

Add Firebase to Android app

After that, we will get the google-service.json file which we need to download as shown in the screenshot below:

Download Google services

Download Google services

After downloading, we need to copy it to the location as shown in the screenshot below:

Move Google service.json to Android

Move Google service.json to Android

Next, we need to open android/build.gradle and add classpath as ‘com.google.gms:google-services:4.2.0’ to dependencies as shown in the code snippet below:

Then, we also need to add apply plugin: ‘com.google.gms.google-services’ configuration to the last line of android/app/build.gradle file as shown in the code snippet below:

Hence if we re-run the app, we will get the following logs in our metro bundler:

Re-run bundle server

Re-run bundle server

Now, if everything works fine, we will get the following result in our Firebase:

Finalize setup Firebase on Android

Finalize setup Firebase on Android

Using Database package

Here, we go back to Feedback.js and import Firebase realtime database package as shown in the code snippet below:

First, we need to create a function named submitForm and call the realtime database as shown in the code snippet below:

Now, if we try to submit the form, we will get an alert as shown in the emulator screenshot below:

Submit data to Firebase

Submit data to Firebase

In order to make a navigation link to the Feedback screen, we need to add a menu option to our Settings.js screen file. For this, we need to use the code from the following code snippet:

Then, we need to use the TouchableOpacity component to implement clickable menu option as shown in the code snippet below:

Here, we have added a navigation configuration to the Feedback screen using the navigate function provided by the navigation option.

Hence, we will get the following result as shown in the emulator screens below:

Setting screen menu

Setting screen menu

 

Sending email with Firebase Cloud Function

The last thing we need to do is to forward a message to the user’s inbox using the Firebase cloud function.

For that we need to install the firebase-tools package globally using NPM as shown below:

Here, we are also logging into Firebase services. As a result, we will get the following success message:

Successfully authenticate to Firebase

Successfully authenticate to Firebase

Then, by running the Firebase init command, we need to choose the required Firebase CLI feature function as shown in the screenshot below:

Setup Firebase function

Setup Firebase function

Next, we need to choose the Firebase project as highlighted in the screenshot below:

Select Firebase project on CLI

Select Firebase project on CLI

Now, we need to open the firebase functions folder by running the following command.

Hence, we can now start implementing the Firebase project. The project structure is shown in the screenshot below:

Firebase CLI project

Firebase CLI project

Now, in order to send the email, we are going to use Sendgrid. So, we need to install it first by running the following command:

Next, we need to open the index. file and import firebase functions and Sendgrid main module. We also need to setup Sendgrid with a new API key as shown in the code snippet below:

Next, we need to call an event observer onCreate that will trigger when new data is added to the database as a new column that we define. Then, we need to call Sendgrid and send an email. The overall coding implementation for this is provided in the code snippet below:

Finally, we send this function to Firebase by using the command provided below:

And now, when we open the Function menu in the Firebase console, we will see the following configuration:

Deploy function to Firebase

Deploy function to Firebase

Hence, if we submit the Feedback form again from our React Native app, we will get an email notification as shown in the emulator screenshot below:

Send email from Sendgrid

Send email from Sendgrid

Hence, we have successfully implemented the Feedback screen with the form submit feature and also setup Firebase along with Sendgrid to send an email notification when the form is submitted.

Conclusion

In this chapter, we learned some important features and packages. First, we learned how to implement a form interface using the Formik package and validate it using the Yup package. Then, we got a detailed insight on how to set up Firebase on both Android and iOS platforms. Lastly, we also learned how to set up the Firebase Cloud Function and send email notifications using Sendgrid.

All code available on Github.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Build Real-World React Native App #11 : Pay For Remove Ads

In this chapter, we are going to apply it in order to implement the Remove Ads feature. First, we are going to implement the Remove ads screen which ...

Build Real-World React Native App #10 : Setup in-App Purchase in iOS

The most awaited and considerably the lengthiest chapter is here. In this chapter, we will learn how to add the in-app purchase service in React ...

Build Real-World React Native App #9 : Implementing Remove Ads Feature

One of the simplest ways to make money from apps is through Advertisements. Hence, we have a popular ads network for mobile that is Admob. There are ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy