如何在 React 嵌套 Div onClick 事件上调用不同的函数?
我有这样的反应中的嵌套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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是在子
onClick
方法中调用event.stopPropagation()
。这将防止家长被呼叫。The solution was to place a call to
event.stopPropagation()
in your childonClick
method. This will prevent the parent from being called.