Skip to content

Commit

Permalink
[lit/react] Fix event binding erroneously being set on custom element…
Browse files Browse the repository at this point in the history
… instance (#4572)
  • Loading branch information
augustjk committed Mar 8, 2024
1 parent 4660f91 commit 5ed30d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-dolls-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/react': patch
---

Fix issue where event handler prop was incorrectly being set on the underlying custom element instance and potentially overriding an existing method/property.
6 changes: 4 additions & 2 deletions packages/react/src/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ const setProperty = <E extends Element>(
) => {
const event = events?.[name];
// Dirty check event value.
if (event !== undefined && value !== old) {
addOrUpdateEventListener(node, event, value as (e?: Event) => void);
if (event !== undefined) {
if (value !== old) {
addOrUpdateEventListener(node, event, value as (e?: Event) => void);
}
return;
}
// But don't dirty check properties; elements are assumed to do this.
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/test/create-component_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ suite('createComponent', () => {
assert.equal(fooEvent2, undefined);
});

// Regression test for https://github.com/lit/lit/issues/4569
test('event prop should not be set on instance', async () => {
const handler = () => {};
render(<BasicElementComponent onFoo={handler} />);
const el = container.querySelector(tagName)!;
assert.notProperty(el, 'onFoo');

// Render again with the same handler
render(<BasicElementComponent onFoo={handler} />);
assert.notProperty(el, 'onFoo');
});

test('can listen to native events', async () => {
let clickEvent!: React.MouseEvent;
render(
Expand Down

0 comments on commit 5ed30d4

Please sign in to comment.