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

Throw error when binding signal to event handler #610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/chilly-planets-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals": minor
---

Throw error when binding a signal to an event handler
16 changes: 14 additions & 2 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,24 @@ hook(OptionsTypes.DIFFED, (old, vnode) => {
updaters = {};
dom._updaters = updaters;
}

for (let prop in props) {
let updater = updaters[prop];
let signal = props[prop];

if (updater === undefined) {
updater = createPropUpdater(dom, prop, signal, renderedProps);
updaters[prop] = updater;
if (prop[0] === "o" && prop[1] === "n") {
throw new Error(
`Can't bind a signal to the ${prop} event listener, use ".value" instead.`
);
} else {
updaters[prop] = updater = createPropUpdater(
dom,
prop,
signal,
renderedProps
);
}
} else {
updater._update(signal, renderedProps);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/preact/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ describe("@preact/signals", () => {
expect(scratch.textContent).to.equal("bar");
});

it("should throw when binding a signal to an event handler", () => {
const callback = signal(() => {});

function App() {
// @ts-ignore
return <button onClick={callback} />;
}

expect(() => {
render(<App />, scratch);
}).to.throw(/Can't bind/);
});

it("should activate signal accessed in render", () => {
const sig = signal(null);

Expand Down