使用 React-dnd 删除组件时未触发 Mouseup 事件

发布于 2025-01-17 06:29:03 字数 427 浏览 1 评论 0原文

我正在开发一个项目,需要使用 React-dnd 包实现拖放功能。这是我能够实现的。但问题是,目标组件(可拖动项目将被放置在其中)包含 mouseup 事件,该事件在每个场景中都会被触发,除非我将任何组件放置在其中。即使在掉落的情况下也需要触发它,但它不会被触发。我也尝试了手动触发,它触发了,但偏移等值并不符合预期。

我共享示例代码沙箱,其中 Dustbin.tsx 包含 mouseup 事件,当任何可拖动组件放入其中时,该事件不会被触发。谢谢。

沙盒链接:https://codesandbox.io/s/黎明-snowflake-ej08d0?file=/src/Dustbin.tsx

I am working on a project where I need to implement drag and drop functionality using React-dnd package. Which is I am able achieve. But the issues is, the target component (where draggable items will be dropped) contains mouseup event which is getting triggered in every scenarios except when I drop any component in it. It needs to be triggered even in the case of drop as well but its not getting triggered. I tried manual triggered as well and it triggered but the values like offsets are not as per expected.

I have share sample code sandbox where Dustbin.tsx contains that mouseup event which is not getting triggered when any draggable component is dropped into it. Thanks.

Sandbox link: https://codesandbox.io/s/dawn-snowflake-ej08d0?file=/src/Dustbin.tsx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2025-01-24 06:29:03

为什么使用onmouseup

当用户在元素上释放鼠标按钮时,就会发生 onmouseup 事件。

如果您想检测容器内是否有东西掉落,请使用 ondrop

当可拖动元素放入元素中时执行 JavaScript:

return (
    <div
      ref={drop}
      style={{ ...style, backgroundColor }}
      onDrop={() => console.log("dropped")}
    >
      {isActive ? "Release to drop" : "Drag a box here"}
    </div>
);

Why using onmouseup?

The onmouseup event occurs when a user releases a mouse button over an element.

If you want to detect if something is dropped inside that container then please use ondrop

Execute a JavaScript when a draggable element is dropped in a element:

return (
    <div
      ref={drop}
      style={{ ...style, backgroundColor }}
      onDrop={() => console.log("dropped")}
    >
      {isActive ? "Release to drop" : "Drag a box here"}
    </div>
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文