Skip to content

Commit

Permalink
Avoid recreating Provider in createDynamicRealmProvider (#6869)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen authored Aug 28, 2024
1 parent 9b45a5a commit 454233b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
3 changes: 1 addition & 2 deletions packages/realm-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* None
* Fixing the `RealmProvider` component when context is created without passing neither a `Realm` instance nor a `Realm.Configuration` to avoid unnecessary recreation of the provider, which was causing "Cannot access realm that has been closed" errors. ([#6842](https://github.com/realm/realm-js/issues/6842), since v0.8.0)

### Compatibility
* React Native >= v0.71.4
Expand Down
12 changes: 5 additions & 7 deletions packages/realm-react/src/RealmProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,15 @@ export function createRealmProviderFromConfig(
* @returns a RealmProvider component that provides context to all context hooks
*/
export function createDynamicRealmProvider(RealmContext: React.Context<Realm | null>): DynamicRealmProvider {
return ({ realm, children, ...configurationProps }) => {
const RealmProviderFromConfig = createRealmProviderFromConfig({}, RealmContext);
return ({ realm, children, ...config }) => {
if (realm) {
if (Object.keys(configurationProps).length > 0) {
if (Object.keys(config).length > 0) {
throw new Error("Cannot use configuration props when using an existing Realm instance.");
}

const RealmProvider = createRealmProviderFromRealm(realm, RealmContext);
return <RealmProvider>{children}</RealmProvider>;
return <RealmContext.Provider value={realm} children={children} />;
} else {
const RealmProvider = createRealmProviderFromConfig({}, RealmContext);
return <RealmProvider {...configurationProps}>{children}</RealmProvider>;
return <RealmProviderFromConfig {...config} children={children} />;
}
};
}
Expand Down

0 comments on commit 454233b

Please sign in to comment.