Call a Function - React Native SDK
On this page
Realm is now Atlas Device SDK – Learn More
The examples in this section demonstrate calling a simple Atlas Function
named sum
that takes two arguments, adds them, and returns the result:
// sum: adds two numbers exports = function(a, b) { return a + b; };
Before You Begin
In an App Services App, define an Atlas Function.
In your client project, initialize the App client.
Then, authenticate a user in your React Native project.
Call a Function
Important
Make sure to sanitize client data to protect against code injection when using Functions.
To call a function, you can either pass its name and arguments to User.callFunction() or call the function as if it were a method on the User.functions property.
import React from 'react'; import {useUser} from '@realm/react'; function Addition() { // Get currently logged in user const user = useUser(); const addNumbers = async (numA: number, numB: number) => { // Call Atlas Function // Method 1: call with User.callFunction() const sumMethod1 = await user?.callFunction('sum', numA, numB); // Method 2: Call with User.function.<Function name>() const sumMethod2 = await user?.functions.sum(numA, numB); // Both methods return the same result console.log(sumMethod1 === sumMethod2); // true }; // ... }