Replies: 3 comments 2 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 |
Beta Was this translation helpful? Give feedback.
-
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> |
Beta Was this translation helpful? Give feedback.
-
In svelte 5 I did like this, but not sure if that's what you meant. <input onclick={inputOnClick ?? undefined} /> |
Beta Was this translation helpful? Give feedback.
-
i.e. I have a
<Component on:click={() => console.log() />
which is called in follwing div...So basically, I want want to make
on:click
on the call level optional but once I pass something it will think it's the actualon:click
function. Hope I've been clear. Thanks!Beta Was this translation helpful? Give feedback.
All reactions