I have tried searching all of the documentation for something about how to use stopPropagation for 'onclick' in Svelte 5, but can find nothing for it other than the legacy stuff. Is there any way to do this in Svelte 5?
I have tried something like this:
<div onclick|stopPropagation={someFunc}>
<button onclick={otherFunc}></button>
</div>
But I get this error: "'onclick|stopPropagation' is not a valid attribute name".
How are you supposed to do this in Svelte 5?
I have tried searching all of the documentation for something about how to use stopPropagation for 'onclick' in Svelte 5, but can find nothing for it other than the legacy stuff. Is there any way to do this in Svelte 5?
I have tried something like this:
<div onclick|stopPropagation={someFunc}>
<button onclick={otherFunc}></button>
</div>
But I get this error: "'onclick|stopPropagation' is not a valid attribute name".
How are you supposed to do this in Svelte 5?
Share Improve this question asked Nov 19, 2024 at 13:07 Lee ManLee Man 7262 gold badges8 silver badges32 bronze badges2 Answers
Reset to default 3I would solve it like this:
<div onclick={(e) => { e.stopPropagation(); someFunc();}}>
<button onclick={otherFunc}></button>
</div>
Modifiers are not supported for event properties. Either call the respective function in the handler directly, or use a higher order function to wrap the handler.
<script>
function someFunc(e) {
e.stopPropagation();
// [other logic]
}
</script>
<button onclick={someFunc}>...
<script>
// extract to a module for reusability
function stopPropagation(handler) {
return e => {
e.stopPropagation();
handler(e);
};
}
function someFunc() { /* ... */ }
</script>
<button onclick={stopPropagation(someFunc)}>...
(There is more info on this in the migration guide.)