-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
on:click "Cannot read property 'apply' of undefined" #4090
Comments
Sort of adjacent to this and also to #4153, I was just thinking about the doc change made in #2944. It feels like in theory we should be able to undo that change, now that event handlers are reactive. Indeed, this works: <script>
import ShoppingCart from './ShoppingCart.svelte';
let cart;
</script>
<ShoppingCart bind:this={cart}/>
<button on:click={cart.empty}>
Empty shopping cart
</button> However, this does not: <script>
import ShoppingCart from './ShoppingCart.svelte';
let cart;
</script>
<button on:click={cart && cart.empty}>
Empty shopping cart
</button>
<ShoppingCart bind:this={cart}/> (The cc @tanhauhau |
What changed between 3.12.1 and 3.13.0 for the code that's generated here is: dispose = listen(button, "click", ctx.handler); became dispose = listen(button, "click", function () {
ctx.handler.apply(this, arguments);
}); Previously, we were just attaching I'm not sure whether this change was intended to fix something else, or whether it was just the result of other structured code generation changes. If it were possible to change this back to |
we change from why we wrapped it in in summary is that instead of detach and reattach event listeners whenever |
Things like this make me impatient for null-coelescing operators. I'll look deeper after the holiday. |
Removed: > Note that we can't do `{cart.empty}` since `cart` is `undefined` when the button is first rendered and throws an error. It fixed in 3.16.7 Issue: sveltejs#4090
Describe the bug
Click handler throws an exception if handler function is stored as a variable and that variable is set somewhere in the
<script>
section.We found this while attempting to fix #4087 via PR #4088.
To Reproduce
See repl:
https://svelte.dev/repl/7433e605325d4b4b87ea5522f5a5ff61?version=3.16.3
The exceptions started occurring from
v3.13.0
onward.Using same repl with older compiler produces expected results (no exceptions):
https://svelte.dev/repl/7433e605325d4b4b87ea5522f5a5ff61?version=3.12.1
Expected behavior
Click event shouldn't do anything (i.e. no exception).
Severity
Low; though it does violate the expectations for events on normal DOM elements.
Additional context
Offending generated code is:
The text was updated successfully, but these errors were encountered: