-
Notifications
You must be signed in to change notification settings - Fork 88
(DOCSP-27000): Call a Function for React Native #2535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Readability for Commit Hash: 5d0632f You can see any previous Readability scores (if they exist) by looking Flesch Reading Ease scores for changed documents:
The following table can be helpful in assessing the readability score of a document.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly good. I think we do need to show how to add types to the useUser
hook in order to define custom function types, data types and profile types.
// ... | ||
return loggedIn ? ( | ||
<View> | ||
<UserProvider> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also provide a fallback
component to the UserProvider
. Then your wrapper above would look like so:
<AppProvider appId={}>
<UserProvider fallback={<AnonLogin/>}>
<MyApp/>
</UserProvider>
</AppProvider>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The UserProvider will not render its children until there is a current user set on the app object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The UserProvider will not render its children until there is a current user set on the app object.
yeah it took me an hour+ of debugging to figure that out 😅
since this code isn't in the example i don't think we need to include that here.
tho when we @realm/reactify the auth pages, this pattern will be important to explain (cc @krollins-mdb)
// Call Atlas Function | ||
|
||
// Method 1: call with User.callFunction() | ||
const sumMethod1 = await user?.callFunction('sum', numA, numB); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const sumMethod1 = await user?.callFunction('sum', numA, numB); | |
const sumMethod1 = await user.callFunction('sum', numA, numB); |
I need to get a fix out for this immediately. The user
from useUser
should always be set and never undefined
or null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, i'd rather keep this example as is til that's fixed since the code will throw a TS error if missing ?
// Method 2: Call with User.function.<Function name>() | ||
const sumMethod2 = await user?.functions.sum(numA, numB); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, this would be the preferred syntax. But we need to show an example of how to type this. This can be done on the hook itself.
const user = userUser<FunctionTypes, CustomDataType, UserProfileDataType>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm confused by the code example and how it relates to calling a function. could you explain a bit more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you declare the FunctionTypes as so:
type FunctionTypes = {
sum: (number, number) => number;
};
const Component = () => {
const user = userUser<FunctionTypes>();
const sum = user.functions.sum(12, 14); // <- this is now typed
const sum = user.functions.foo(12, 14); // <- this will now give a type error
const sum = user.functions.sum("foo", "bar"); // <- this will also now give a type error
}
Then sum
will be typed for user.function.sum
with typed arguments and a return value.
CustomDataType
and UserProfileDataType
can be used to add any extra user data and have that typed as well.
The thing is, it is currently not possible to infer what a user has configured in App Services, so they would have to define the types themselves in order to have proper typing in their application. Ideally we should have a way to generate this for our users, but for now, this is the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not very well documented. There is an example of this in realm-web
. The type is set on the creation of Realm.App
and in turn is applied to any user
object returned from that app instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, thanks for clarifying. this is quite useful. will include in the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, after spending a bit of time with this, it seems that the API is slightly different from what's in the above example.
these differences are:
- i need to provide all 3 types to useUser<FunctionTypes, CustomDataType, Realm.DefaultUserProfileData> to not cause a TS error.
- FunctionTypes must be a union with
Realm.DefaultFunctionsFactory
to work. for example:
type FunctionTypes = {
sum: (a: number, b: number) => number;
} & Realm.DefaultFunctionsFactory;
for these reasons, i'd rather not include this in the docs yet, though i think longer-term it absolutely makes sense to include in the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can see the working version with the types in this commit checkpoint: RN types on useUser.
going to revert those changes in the HEAD of the branch.
could you provide an example of how to do this? didn't see anything in the @realm/react unit test suite or elsewhere. |
8e4bbc6
to
14e60f7
Compare
This reverts commit 14e60f7.
## 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]>
## 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]>
Pull Request Info
Call a Function for React Native w @realm/react
Jira
Staged Changes
Reminder Checklist
If your PR modifies the docs, you might need to also update some corresponding
pages. Check if completed or N/A.
Review Guidelines
REVIEWING.md