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

fix(react): trigger useComponentValue on deleted records #1959

Merged
merged 3 commits into from
Nov 30, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tasty-balloons-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/react": patch
---

Fixed an issue where `useComponentValue` would not detect a change and re-render if the component value was immediately removed.
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@
"clean": "pnpm run clean:js",
"clean:js": "rimraf dist",
"dev": "tsup --watch",
"test": "tsc --noEmit && vitest --run",
"test:ci": "pnpm run test"
"test": "tsc --noEmit && vitest",
"test:ci": "tsc --noEmit && vitest --run"
},
"dependencies": {
"@latticexyz/recs": "workspace:*",
22 changes: 22 additions & 0 deletions packages/react/src/useComponentValue.test.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@ describe("useComponentValue", () => {
removeComponent(Position, entity);
});
expect(result.current).toBe(undefined);

act(() => {
setComponent(Position, entity, { x: 0, y: 0 });
});
expect(result.current).toEqual({ x: 0, y: 0 });
});

it("should re-render only when Position changes for entity", () => {
@@ -70,6 +75,23 @@ describe("useComponentValue", () => {
expect(result.current).toBe(undefined);
});

it("should re-render when Position is removed", () => {
const entity = createEntity(world, [withValue(Position, { x: 1, y: 1 })]);

const { result } = renderHook(() => useComponentValue(Position, entity));
expect(result.current).toEqual({ x: 1, y: 1 });

act(() => {
removeComponent(Position, entity);
});
expect(result.current).toBe(undefined);

act(() => {
setComponent(Position, entity, { x: 0, y: 0 });
});
expect(result.current).toEqual({ x: 0, y: 0 });
});

it("should return default value when Position is not set", () => {
const entity = createEntity(world);

2 changes: 1 addition & 1 deletion packages/react/src/useComponentValue.ts
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ export function useComponentValue<S extends Schema>(
setValue(entity != null ? getComponentValue(component, entity) : undefined);
if (entity == null) return;

const queryResult = defineQuery([Has(component)], { runOnInit: false });
const queryResult = defineQuery([Has(component)], { runOnInit: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know if this works for adding/deleting records after the initial delete? I suspect this might just help with the first one, but I could be wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix still has useComponentValue update if the same record is added after they're deleted. In MUDv1 where the issue had already existed, the issue happened when a record that already existed on init was then deleted, which useComponentValue didn't update without { runOnInit: true }. If a record didn't already exist upon init, was created, and then deleted, then the hook correctly updated even without the fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you wanna see if you can wire up a test to show this behavior and so we don't regress on this?

const subscription = queryResult.update$.subscribe((update) => {
if (isComponentUpdate(update, component) && update.entity === entity) {
const [nextValue] = update.value;
Loading