From 15d08bbf0b02df8228457b1a96d4adb8ed46b876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 28 Aug 2024 10:56:44 +0200 Subject: [PATCH] Avoid recreating Provider in createDynamicRealmProvider --- packages/realm-react/CHANGELOG.md | 3 +-- packages/realm-react/src/RealmProvider.tsx | 12 +++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/realm-react/CHANGELOG.md b/packages/realm-react/CHANGELOG.md index 6e5cbc5d592..951fc91e2f8 100644 --- a/packages/realm-react/CHANGELOG.md +++ b/packages/realm-react/CHANGELOG.md @@ -7,8 +7,7 @@ * None ### Fixed -* ([#????](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 diff --git a/packages/realm-react/src/RealmProvider.tsx b/packages/realm-react/src/RealmProvider.tsx index 29162c1be7c..2915fdf1132 100644 --- a/packages/realm-react/src/RealmProvider.tsx +++ b/packages/realm-react/src/RealmProvider.tsx @@ -208,17 +208,15 @@ export function createRealmProviderFromConfig( * @returns a RealmProvider component that provides context to all context hooks */ export function createDynamicRealmProvider(RealmContext: React.Context): 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 {children}; + return } else { - const RealmProvider = createRealmProviderFromConfig({}, RealmContext); - return {children}; + return ; } }; }