sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.85k stars 4.24k forks source link

can't set onclick via attribute #2048

Closed halfnelson closed 5 years ago

halfnelson commented 5 years ago

https://v3.svelte.technology/repl?version=3.0.0-alpha27&gist=9351806a082bdb1d1564e78eb423f27c

setting onclick={somefunction} results in the dom containing onclick="function somefunction() ...".

Setting via spread props works. This is because the first method generates setAttribute call and the second generates a setAttributes call. The setAttributes call has a codepath that will set the value on the element using its property if it can whereas setAttribute always uses the dom's node.setAttribute() call.

Should these be consistent? is this expected?

thgh commented 5 years ago

It works when using on:click={change} (note the colon : )

halfnelson commented 5 years ago

Thank you. Although if I recall on:click does an addEventListener('click',...). Not sure that helps if the element is a WebComponent that wants a callback and does expose a custom event. eg <stockticker-web-component onConnectionError="{errorHandler}"/>

Conduitry commented 5 years ago

I'm going through and trying to clean up old issues, and I think this one falls under 'that's just how web components work'. Afaik you can only pass strings to web components via attributes when you're using them in regular HTML, and I don't see a different non-confusing way we could handle this.