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

Nullish event handler produces TypeError #15081

Open
vkurko opened this issue Jan 21, 2025 · 1 comment
Open

Nullish event handler produces TypeError #15081

vkurko opened this issue Jan 21, 2025 · 1 comment

Comments

@vkurko
Copy link

vkurko commented Jan 21, 2025

Describe the bug

Some event handlers, such as onmouseenter or onmouseleave, throw an error when set to null/undefined instead of not registering the event handler.

Reproduction

https://svelte.dev/playground/fa643cc4e0ed4434bb6ba9bc5a5a8ec0?version=5.19.1

Logs

TypeError
TypeError: Cannot read properties of undefined (reading 'call')
    at eval (playground:output:2036:21)
    at without_reactive_context (playground:output:2004:11)
    at HTMLButtonElement.target_handler (playground:output:2035:12)

System Info

Svelte 5.19.1 (Playground)

Severity

blocking an upgrade

@Ocean-OS
Copy link
Contributor

The reason for that is if you look at the compiled output, you'll notice that for onclick, it assigns a property of the button to the event handler, while with the other event, it calls a function. In the function, it runs Element.addEventListener, which throws if the provided handler is not a function. You shouldn't have a nullish event handler (partly because there's no reason to).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants