-
-
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
Change event handler dynamically #3040
Comments
Hi everyone, Just wanted to add my thoughts on why conditional events are useful. I'm playing around with building a Slider component. When you This was my first (ever) try: <div class='slider'>
<div class='slider__handle' on:mousedown={() => interacting = true } />
</div>
<svelte:body on:mouseup={interacting ? () => { interacting = false; } : undefined } /> It would be brilliant if event listeners were reactive, with (Full code: https://svelte.dev/repl/f91cb7cc81c44771b3260bde7f12013b?version=3.6.9) |
P.S.: As a workaround, I can use: import { onDestroy } from 'svelte';
$: interacting ?
document.addEventListener('mouseup', end) :
document.removeEventListener('mouseup', end);
onDestroy(() => {
document.removeEventListener('mouseup', mouseup);
}); |
If I understand correctly, event handlers are currently evaluated once. And if they were to become reactive, does that mean it negates the you can safely use inline handlers in Svelte part? If that's the case, maybe reactivity should be opt-in, via an event modifier? (e.g. |
Comment from @pngwn on Discord:
|
Thanks! It would be interesting to see more of that example, if @pngwn can share, to understand what they mean with the snippet. If |
Yeah, the handler in question was: const fn = (x, i) => evt => // whatever And yes, this binds the handler to |
Thanks for the additional details. I wrote an example (based on the discussion in #3132) which was, indeed, a bit surprising. I guess I was lacking the correct mental model for how/when event handlers are evaluated in an <ul>
{#each fruits as fruit }
<li on:click={eat(fruit)}>{ fruit }</li>
{/each }
</ul> the event handler got de-synchronized from the item it was initially attached to is unexpected IMHO. (I do understand, however, how keying the |
On wanting a dynamic handler, I found myself wanting to do this: <script>
let number = 1;
let doer = () => number = 2;
function doer_change(){
doer = () => number = 3;
}
</script>
<p>{number}</p>
<button on:click={doer}>Do</button>
<button on:click={doer_change}>Change do</button> It seems intuitive that by clicking the |
@kwangure i guess a workaround for now would be <script>
let number = 1;
let doer = () => number = 2;
function doer_change(){
doer = () => number = 3;
}
let onClick = () => doer();
</script>
<p>{number}</p>
<button on:click={onClick}>Do</button>
<button on:click={doer_change}>Change do</button> |
@tanhauhau Thank you for your wonderful work. |
This is good, but it'd be great to see:
|
This comment has been minimized.
This comment has been minimized.
See #3040 (comment) above. |
@kwangure Oh okay, sorry. |
Svelte 3 is really great work.
I want to change event handler dynamically.
So, I assigned anonymous function to a variable and set this variable to event handler.
But it doesn't works as I expect.
Please see this REPL.
https://svelte.dev/repl/e9cc2a6c4ad64cb997bb557e066a7696?version=3.5.1
I found some workaround, but it's annoying a bit.
Is this behavior correct?
Is there something I missed?
with many thanks.
The text was updated successfully, but these errors were encountered: