Skip to content

Commit 21ee708

Browse files
mongodbencbush
andcommitted
(DOCSP-27000): Call a Function for React Native (#2535)
## Pull Request Info Call a Function for React Native w @realm/react ### Jira - https://jira.mongodb.org/browse/DOCSP-27000 ### Staged Changes - [Call a Function (RN)](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27000/sdk/react-native/app-services/call-a-function/) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) --------- Co-authored-by: Chris Bush <[email protected]>
1 parent 0b6a055 commit 21ee708

File tree

3 files changed

+117
-21
lines changed

3 files changed

+117
-21
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// :snippet-start: call-function
2+
import React from 'react';
3+
import {useUser} from '@realm/react';
4+
// :remove-start:
5+
import {Credentials} from 'realm';
6+
import {useEffect, useState} from 'react';
7+
import {App} from 'realm';
8+
import {AppProvider, UserProvider, useApp} from '@realm/react';
9+
import {render, fireEvent, waitFor} from '@testing-library/react-native';
10+
import {View, Button, Text} from 'react-native';
11+
12+
const APP_ID = 'example-testers-kvjdy';
13+
14+
function AppWrapper() {
15+
return (
16+
<View>
17+
<AppProvider id={APP_ID}>
18+
<MyApp />
19+
</AppProvider>
20+
</View>
21+
);
22+
}
23+
24+
function MyApp() {
25+
const [loggedIn, setLoggedIn] = useState(false);
26+
const app = useApp();
27+
28+
useEffect(() => {
29+
app.logIn(Credentials.anonymous()).then(user => user && setLoggedIn(true));
30+
}, []);
31+
// ...
32+
return loggedIn ? (
33+
<View>
34+
<UserProvider>
35+
<Text>Foo!</Text>
36+
<Addition />
37+
</UserProvider>
38+
</View>
39+
) : null;
40+
}
41+
42+
let higherScopedSum: number;
43+
// :remove-end:
44+
45+
function Addition() {
46+
// Get currently logged in user
47+
const user = useUser();
48+
49+
const addNumbers = async (numA: number, numB: number) => {
50+
// Call Atlas Function
51+
52+
// Method 1: call with User.callFunction()
53+
const sumMethod1 = await user?.callFunction('sum', numA, numB);
54+
55+
// Method 2: Call with User.function.<Function name>()
56+
const sumMethod2 = await user?.functions.sum(numA, numB);
57+
58+
// Both methods return the same result
59+
console.log(sumMethod1 === sumMethod2); // true
60+
// :remove-start:
61+
expect(sumMethod1).toBe(sumMethod2);
62+
higherScopedSum = sumMethod1 as number;
63+
// :remove-end:
64+
};
65+
// ...
66+
// :remove-start:
67+
return <Button onPress={() => addNumbers(1, 2)} testID='test-function-call' title='Test Me!' />;
68+
// :remove-end:
69+
}
70+
// :snippet-end:
71+
72+
afterEach(async () => await App.getApp(APP_ID).currentUser?.logOut());
73+
74+
test('Call Atlas Function', async () => {
75+
const {getByTestId} = render(<AppWrapper />);
76+
77+
const button = await waitFor(() => getByTestId('test-function-call'));
78+
fireEvent.press(button);
79+
await waitFor(() => expect(higherScopedSum).toBe(3));
80+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import {useUser} from '@realm/react';
3+
4+
function Addition() {
5+
// Get currently logged in user
6+
const user = useUser();
7+
8+
const addNumbers = async (numA: number, numB: number) => {
9+
// Call Atlas Function
10+
11+
// Method 1: call with User.callFunction()
12+
const sumMethod1 = await user?.callFunction('sum', numA, numB);
13+
14+
// Method 2: Call with User.function.<Function name>()
15+
const sumMethod2 = await user?.functions.sum(numA, numB);
16+
17+
// Both methods return the same result
18+
console.log(sumMethod1 === sumMethod2); // true
19+
};
20+
// ...
21+
}

source/sdk/react-native/app-services/call-a-function.txt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,26 @@ named ``sum`` that takes two arguments, adds them, and returns the result:
2121
return a + b;
2222
};
2323

24-
.. _react-native-call-a-function-by-name:
25-
26-
Call a Function by Name
27-
-----------------------
24+
Prerequisites
25+
-------------
2826

29-
.. include:: /includes/important-sanitize-client-data-in-functions.rst
27+
Before You Begin
28+
----------------
3029

31-
To call a function, you can either pass its name and arguments to
32-
``User.callFunction()`` or call the function as if it was a method on the
33-
:js-sdk:`User.functions <Realm.User.html#functions>` property.
30+
#. In an App Services App, :ref:`define an Atlas Function <define-a-function>`.
31+
#. In your client project, :ref:`initialize the App client <react-native-connect-to-mongodb-realm-backend-app>`.
32+
#. Then, :ref:`authenticate a user <react-native-authenticate-users>` in your React Native project.
3433

35-
.. note:: Link a MongoDB Atlas Data Source
36-
37-
This example requires a Realm app with a linked
38-
:ref:`Atlas data source <data-sources>`. Replace
39-
``<appId>`` in the code with your App ID, which you can find in the
40-
left navigation menu of the App Services UI.
34+
.. _react-native-call-a-function-by-name:
4135

42-
.. literalinclude:: /examples/generated/node/call-a-function.snippet.call-a-function-by-name.js
43-
:language: javascript
44-
36+
Call a Function
37+
---------------
4538

46-
When you run the code sample, your output should resemble the following:
39+
.. include:: /includes/important-sanitize-client-data-in-functions.rst
4740

48-
.. code-block:: none
41+
To call a function, you can either pass its name and arguments to
42+
:js-sdk:`User.callFunction() <Realm.User.html#functions>` or call the function
43+
as if it were a method on the :js-sdk:`User.functions <Realm.User.html#functions>` property.
4944

50-
Using the "functions.sum()" method: the sum of 2 + 3 = 5
51-
Using the "callFunction()" method: the sum of 2 + 3 = 5
45+
.. literalinclude:: /examples/generated/react-native/ts/atlas-functions.test.snippet.call-function.tsx
46+
:language: typescript

0 commit comments

Comments
 (0)