Replies: 3 comments 4 replies
|
Why not wrap in an {#if variableToCheck}
<div on:click> // <= I want to make this `on:click` arg conditional
<Component />
</div>
{/if}Or you can check inside the component the conditional variable to handle or not |
1 reply
|
Do you meant to conditionally check and add the event listener? The <script script="ts">
import { onMount } from 'svelte';
let element: HTMLElement;
onMount(() => {
if (conditional) {
element.addEventListener('click', () => {
// ...
})
}
})
</script>
<div bind:this={element}>
<Component />
</div>Unless you want to do the conditional check in the function itself where the event listener is already added. <div on:click={() => {
if (conditional) {
// ...
}
}}>
<Component />
</div> |
1 reply
|
In svelte 5 I did like this, but not sure if that's what you meant. <input onclick={inputOnClick ?? undefined} /> |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
i.e. I have a
<Component on:click={() => console.log() />which is called in follwing div...So basically, I want want to make
on:clickon the call level optional but once I pass something it will think it's the actualon:clickfunction. Hope I've been clear. Thanks!All reactions