sveltekit - No stopPropagation for Svelte 5? - Stack Overflow

I have tried searching all of the documentation for something about how to use stopPropagation for 

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 badges
Add a comment  | 

2 Answers 2

Reset to default 3

I 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.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745560558a4633082.html

相关推荐

  • sveltekit - No stopPropagation for Svelte 5? - Stack Overflow

    I have tried searching all of the documentation for something about how to use stopPropagation for 

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信