Skip to content

Commit

Permalink
Make useObject compatible with undefined objects (#6115)
Browse files Browse the repository at this point in the history
Co-authored-by: LJ <[email protected]>
  • Loading branch information
takameyer and elle-j authored Sep 4, 2023
1 parent 2c03676 commit ca1d2b9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
6 changes: 3 additions & 3 deletions COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
| 11.2.0 | >= 0.4.0 | >= 0.70.0 | 47 || >= 7 | >= 13 (excluding 19) |
| 11.1.0 | >= 0.4.0 | >= 0.70.0 | 47 || >= 7 | >= 13 |
| 11.0.0 | >= 0.4.0 | >= 0.70.0 | 47 || >= 7 | >= 13 |
| 11.0.0-rc.2 | >= 0.4.0 | >= 0.70.0 | N/A || >= 7 | >= 13 |
| 11.0.0-rc.1 | >= 0.4.0 | >= 0.69.0 < 0.70.0 | 46 || >= 7 | >= 13 |
| 11.0.0-rc.0 | >= 0.4.0 | >= 0.66.0 < 0.69.0 | 45 || >= 7 | >= 13 |
| 11.0.0-rc.2 | >= 0.3.0 | >= 0.70.0 | N/A || >= 7 | >= 13 |
| 11.0.0-rc.1 | >= 0.3.0 | >= 0.69.0 < 0.70.0 | 46 || >= 7 | >= 13 |
| 11.0.0-rc.0 | >= 0.3.0 | >= 0.66.0 < 0.69.0 | 45 || >= 7 | >= 13 |
| 10.24.0 | >= 0.3.0 | >= 0.64.0 | 44 || >= 7 | >= 13 |
| 10.23.0 | >= 0.3.0 | >= 0.64.0 | 44 || >= 7 | >= 13 |
| 10.22.0 | >= 0.3.0 | >= 0.64.0 | 44 || >= 7 | >= 13 |
Expand Down
12 changes: 4 additions & 8 deletions packages/realm-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* None
* `useObject` will now re-render if the result of `objectForPrimaryKey` is `null` or `undefined`. ([#6101](https://github.com/realm/realm-js/issues/6101))

### Compatibility
* React Native >= v0.71.4
* Realm Studio v14.0.0.
* File format: generates Realms with format v23 (reads and upgrades file format v5 or later for non-synced Realm, upgrades file format v10 or later for synced Realms).
* React Native >= v0.68.0
* Realm >= 11.0.0

### Internal
<!-- * Either mention core version or upgrade -->
<!-- * Using Realm Core vX.Y.Z -->
<!-- * Upgraded Realm Core from vX.Y.Z to vA.B.C -->
* Removed `11.0.0-rc` from compatible versions.

## 0.6.0 (2023-08-23)

Expand Down
2 changes: 1 addition & 1 deletion packages/realm-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
},
"peerDependencies": {
"react": ">=17.0.2",
"realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0-rc || ^11.0.0"
"realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0"
},
"optionalDependencies": {
"@babel/runtime": ">=7",
Expand Down
17 changes: 17 additions & 0 deletions packages/realm-react/src/__tests__/useObjectRender.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,23 @@ describe("useObject: rendering objects with a Realm.List property", () => {
fireEvent.press(rerenderButton);
expect(objectChangeCounter).toHaveBeenCalledTimes(expectedCount);
});
it("will rerender when the realm database is cleared", async () => {
await setupTest();

const initialCount = 2;

expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount);

await act(async () => {
testRealm.write(() => {
testRealm.deleteAll();
});
forceSynchronousNotifications(testRealm);
});


expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount + 1);
});
});

describe("rendering objects with a Realm.List property", () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/realm-react/src/__tests__/useQueryRender.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,23 @@ describe.each`
fireEvent.press(rerenderButton);
expect(queryObjectChangeCounter).toHaveBeenCalledTimes(1);
});

it("will rerender when the realm database is cleared", async () => {
await setupTest({ queryType });

const initialCount = 1;

expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount);

await act(async () => {
testRealm.write(() => {
testRealm.deleteAll();
});
forceSynchronousNotifications(testRealm);
});

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount + 1);
});
});
6 changes: 3 additions & 3 deletions packages/realm-react/src/useObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createUseObject(useRealm: () => Realm) {
// Ref: https://github.com/facebook/react/issues/14490
const cachedObjectRef = useRef<null | CachedObject>(null);

if (cachedObjectRef.current === null) {
if (!cachedObjectRef.current) {
cachedObjectRef.current = createCachedObject({
object: originalObject ?? null,
realm,
Expand All @@ -81,7 +81,7 @@ export function createUseObject(useRealm: () => Realm) {
// Re-instantiate the cachedObject if the primaryKey has changed or the originalObject has gone from null to not null
if (
!arePrimaryKeysIdentical(primaryKey, primaryKeyRef.current) ||
(originalObjectRef.current === null && originalObject !== null)
(!originalObjectRef.current && originalObject)
) {
cachedObjectRef.current = createCachedObject({
object: originalObject ?? null,
Expand Down Expand Up @@ -138,7 +138,7 @@ export function createUseObject(useRealm: () => Realm) {
}, [realm, type, forceRerender]);

// If the object has been deleted or doesn't exist for the given primary key, just return null
if (object === null || object?.isValid() === false) {
if (!object?.isValid()) {
return null;
}

Expand Down

0 comments on commit ca1d2b9

Please sign in to comment.