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

Make useObject compatible with undefined objects #6115

Merged
merged 5 commits into from
Sep 4, 2023
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
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 @@
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);
});


Check failure on line 455 in packages/realm-react/src/__tests__/useObjectRender.test.tsx

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
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
Loading