Skip to content
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

RJS-2648: Add ability for Realm instance to be used in RealmProvider #6714

Merged
merged 32 commits into from
Jun 17, 2024

Conversation

gagik
Copy link
Contributor

@gagik gagik commented Jun 10, 2024

What, How & Why?

@realm/devdocs

What, How

  1. The default RealmProvider (or any empty provider created from createRealmContext()) is the most generic provider which has mutually exclusive PropTypes that accepts either an existing realm or Realm configuration fields as props.
import { RealmProvider } from `@realm/react`;
...
return <>
  {/* Valid */}
  <RealmProvider realm={new Realm(...)}> ... </RealmProvider>
  {/* Valid */}
  <RealmProvider schema={[catSchema]} path="test.realm" realmRef={ref}> ... </RealmProvider>
  {/* Invalid, would raise a type exception */}
  <RealmProvider realm={new Realm()} schema={[catSchema]}> ... </RealmProvider>
</>
  1. Provider created from using an existing Realm instance, i.e. createRealmContext(realm) is a simple wrapper with no props, just children. NOTE: This provider is not necessary, you can use the hooks without it as long as you import from the correct context!
const { RealmProvider, useRealm } = createRealmContext(realm);
...
<>
   {/* This provider is valid but not needed */}
  <RealmProvider> ... </RealmProvider>
  <AnotherComponent>
  {/*  When using createRealmContext(realm) with an existing Realm,
       you can use the hooks outside of the scope of the provider! */}
  </AnotherComponent>
</>
  1. Provider created from a configuration, i.e. createRealmContext({schema: [...]) allows you to use all the Realm configuration settings as well as the existing special @realm/react props.
const { RealmProvider } = createRealmContext({schema: [catSchema]});
...
<RealmProvider closeOnUnmount={false} realmRef={ref} path="thing.realm" schema={...}> ... </RealmProvider>

Nesting

If two default providers are nested, the deepest provider will take precedence, i.e.

<RealmProvider realm={realmA}>
     <RealmProvider realm={realmB}>
            {/* Running useRealm() here will return realmB */}
      </RealmProvider>
</RealmProvider>

Same applies for configuration-based providers.

Why?

Unlocks new usecases and abilities for the RealmProvider, where one might want to use their own managed Realm in the context.

This closes #6283

☑️ ToDos

  • 📝 Changelog entry
  • 📝 Compatibility label is updated or copied from previous entry
  • 📝 Update COMPATIBILITY.md
  • 🚦 Tests
  • 📦 Updated internal package version in consuming package.jsons (if updating internal packages)
  • 📱 Check the React Native/other sample apps work if necessary
  • 💥 Breaking label has been applied or is not necessary

Copy link
Contributor Author

@gagik gagik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some things worth discussing.

packages/realm-react/src/RealmProvider.tsx Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
@gagik gagik marked this pull request as ready for review June 12, 2024 10:50
Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see this feature coming alive 🎉 Leaving my comments from a first pass. (Haven't gone through all of it yet.)

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/index.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/index.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/index.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/index.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
Copy link
Member

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like they way you're expressing the two mutually exclusive modes of the provider and also tying that back with the createRealmContext - Great stuff! 🎉 I have minor comments, but nothing blocking a merge.

@kraenhansen
Copy link
Member

I think it would be good to have a few tests using @ts-expect-error comments to assert the mutual exclusivity of the different props.

Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work and nicely documented! 🙂

CHANGELOG.md Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/__tests__/RealmProvider.test.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/__tests__/RealmProvider.test.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/RealmProvider.tsx Show resolved Hide resolved
gagik and others added 4 commits June 17, 2024 09:56
Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
@gagik gagik merged commit cbd975a into main Jun 17, 2024
8 checks passed
@gagik gagik deleted the gagik/realm-instance-provider branch June 17, 2024 14:32
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2024
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@realm/react: Passing a realm as prop to RealmProvider
4 participants