React Native
...
Parse SDK (REST)
Cloud Functions

Triggers

15min

Using Cloud Functions in a React Native App

Introduction

In this guide, you will learn how to use the Parse Cloud Code Functions from a React Native App. You will see examples of triggers implemented using cloud functinos, and also check how to implement a React Native component using these functions and Back4App.

Prerequisites

To complete this tutorial, you will need:

Goal

Run Parse Cloud Code on Back4App from a React Native App.

1 - Understanding Cloud Code Functions

In your applications, there will be times that you will need to perform certain data operations or heavy processing that shouldn’t be done on mobile devices. For these cases, you may use Cloud Code functions, that run directly on your Parse server and are called using Parse API. Note that this also enables changing some part of your app’s logic without needing to release a new version on the app stores, which can be useful in some cases.

There are two main types of Cloud Code functions, generic functions, using define and holding any type of code you want, and triggers, that are fired when certain events occur in your Parse server automatically.

In the next step, you will be presented with examples for most of these function types, which can be created and deployed directly in your Back4App dashboard or via CLI. You can check how to perform this in our Cloud functions starter guide.

2 - Cloud Code Reference

Cloud Functions

Cloud Functions


Response:

1 { "result": 3.5 }

Save Triggers

beforeSave
afterSave


Delete Triggers

beforeDelete
afterDelete


File Triggers

beforeSaveFile
afterSaveFile
beforeDeleteFile
afterDeleteFile


Find Triggers

beforeFind
afterFind


Session Triggers

beforeLogin
afterLogout


3 - Using Cloud Code from a React Native component

Let’s now create an example Parse Cloud Code function and call it inside a component in React Native, with a simple interface having a label showing the function result and also a form for adding new objects.

Our cloud function will calculate the average of the ratings of every movie Review object in our app, which is a Parse.Object class composed of text, rate and movie fields. Here is the function code:

JS


To call this cloud function in React Native, you need to use the Parse.Cloud.run method, passing as an argument the function name and also any necessary parameters inside an object.

JavaScript
TypeScript


We can also enforce that the reviews’ texts are kept short by using a beforeSave trigger function for the Review object. Here is the function code:

JavaScript


This is how the full component code is laid out:

JavaScript
TypeScript


This is how the component should look like after rendering and querying by one of the query functions:

Document image


Conclusion

At the end of this guide, you learned how Parse Cloud Code functions work and how to perform them on Back4App from a React Native App. In the next guide, you will learn how to work with Users in Parse.

Updated 29 Mar 2024
Did this page help you?