如何在 React 嵌套 Div onClick 事件上调用不同的函数?

发布于 2025-01-18 14:01:41 字数 362 浏览 2 评论 0原文

我有这样的反应中的嵌套div:

<div onClick={() => fn_01()}>
   <div onClick={() => fn_02()}>
   </div>
</div>

当用户单击这些div时,我想调用不同的功能。但是,当我单击第一个Div时,它称为fn_01(),当我单击第二个Div时,它也会调用fn_01()。我该如何解决这个问题。

我正在使用React项目中的功能组件。我还实现了类组件,并使用this。调用这些方法,这也行不通。我需要在功能组件中进行这项工作

I have a nested div in react like this:

<div onClick={() => fn_01()}>
   <div onClick={() => fn_02()}>
   </div>
</div>

I want to call different function when user click those div. But when I click the 1st div it called fn_01() and when I click the 2nd div it also call fn_01(). How can I solve this issue.

I am using functional component in my react project. I also implement Class component and call those method using this., which doesn't work also. I need this work in Functional Component

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

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

发布评论

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

评论(1

无敌元气妹 2025-01-25 14:01:41

解决方案是在子 onClick 方法中调用 event.stopPropagation()。这将防止家长被呼叫。

The solution was to place a call to event.stopPropagation() in your child onClick method. This will prevent the parent from being called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文