diff --git a/packages/realm-react/package.json b/packages/realm-react/package.json index 10228b6e6e..e47d7e8370 100644 --- a/packages/realm-react/package.json +++ b/packages/realm-react/package.json @@ -38,7 +38,8 @@ "prettier": "^2.3.2", "react": "^17.0.2", "react-native": "^0.65.1", - "react-test-renderer": "^17.0.2" + "react-test-renderer": "^17.0.2", + "realm": "*" }, "peerDependencies": { "react": ">=16.8.0", diff --git a/packages/realm-react/src/cachedObject.ts b/packages/realm-react/src/cachedObject.ts index bfaead4a80..9295aab823 100644 --- a/packages/realm-react/src/cachedObject.ts +++ b/packages/realm-react/src/cachedObject.ts @@ -21,11 +21,11 @@ import { createCachedCollection } from "./cachedCollection"; /** * Arguments object for `cachedObject`. */ -type CachedObjectArgs = { +type CachedObjectArgs = { /** * The {@link Realm.Object} to proxy */ - object: T | null; + object: Realm.Object | null; /** * The {@link Realm} instance */ @@ -49,11 +49,11 @@ type CachedObjectArgs = { * @param args - {@link CachedObjectArgs} object arguments * @returns Proxy object wrapping the {@link Realm.Object} */ -export function createCachedObject({ +export function createCachedObject({ object, realm, updateCallback, -}: CachedObjectArgs): { object: T | null; tearDown: () => void } { +}: CachedObjectArgs): { object: Realm.Object | null; tearDown: () => void } { const listCaches = new Map(); const listTearDowns: Array<() => void> = []; // If the object doesn't exist, just return it with an noop tearDown @@ -64,7 +64,7 @@ export function createCachedObject({ // This Proxy handler intercepts any accesses into properties of the cached object // of type `Realm.List`, and returns a `cachedCollection` wrapping those properties // to allow changes in the list to trigger re-renders - const cachedObjectHandler: ProxyHandler = { + const cachedObjectHandler: ProxyHandler = { get: function (target, key) { const value = Reflect.get(target, key); // Pass methods through @@ -92,14 +92,14 @@ export function createCachedObject({ }; const cachedObjectResult = new Proxy(object, cachedObjectHandler); - const listenerCallback: Realm.ObjectChangeCallback = (obj, changes) => { + const listenerCallback: Realm.ObjectChangeCallback = (obj, changes) => { if (changes.deleted) { updateCallback(); } else if (changes.changedProperties.length > 0) { // Don't force a second re-render if any of the changed properties is a Realm.List, // as the List's cachedCollection will force a re-render itself const anyListPropertyModified = changes.changedProperties.some((property) => { - return obj[property as keyof T] instanceof Realm.List; + return obj[property] instanceof Realm.List; }); const shouldRerender = !anyListPropertyModified; diff --git a/packages/realm-react/src/useObject.tsx b/packages/realm-react/src/useObject.tsx index 53ad840552..8af5825e1d 100644 --- a/packages/realm-react/src/useObject.tsx +++ b/packages/realm-react/src/useObject.tsx @@ -61,7 +61,7 @@ export function createUseObject(useRealm: () => Realm) { // When this is implemented, remove `?? null` () => createCachedObject({ - object: realm.objectForPrimaryKey(type, primaryKey) ?? null, + object: realm.objectForPrimaryKey(type, primaryKey) ?? null, realm, updateCallback: forceRerender, }), @@ -79,6 +79,6 @@ export function createUseObject(useRealm: () => Realm) { } // Wrap object in a proxy to update the reference on rerender ( should only rerender when something has changed ) - return new Proxy(object, {}); + return new Proxy(object, {}) as T & Realm.Object; }; }