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-2887: Avoid recreating Provider in createDynamicRealmProvider #6869

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading