[英]How can I prevent parent events from being triggered
I have a pattern I run into quite a lot in React and I can't seem to figure out what the best way around it is.我有一个在 React 中经常遇到的模式,但我似乎无法弄清楚最好的解决方法是什么。
Given following basic code example:给出以下基本代码示例:
<Card onClick={this.toggleCollapsedModules}>
<CardBody>
<Row className="align-items-center">
<Col xl={2} lg={3}>
<h1>Test</h1>
</Col>
<Col xl={9} lg={9}>
<h1>Description</h1>
</Col>
<Col lg={1} sm={12} xs={12}>
<div className="float-right mr-3">
<Info
onClick={this.togglePricingModal}
className="icon-lg"
style={{ cursor: 'pointer' }}>
</Info>
</div>
</Col>
</Row>
</CardBody>
</Card>
If I click anywhere on my <Card>
element, the toggleCollapsedModules
is triggered which opens the card to display more information below.如果我单击
<Card>
元素上的任意位置,则会触发toggleCollapsedModules
,这会打开卡片以在下面显示更多信息。 The thing is the action within the icon that toggles a modal, is also triggering the parent toggleCollapsedModules
which is normal because the icon is part of the <Card>
hierarchy.事情是图标内切换模式的动作,也触发了父
toggleCollapsedModules
,这是正常的,因为图标是<Card>
层次结构的一部分。
How can I prevent the parent toggle if and only if I click on the icon itself?当且仅当我单击图标本身时,如何防止父切换?
This is the basic view without any triggers.这是没有任何触发器的基本视图。
This is when the icon is clicked (you can see the modal got opened but the parent click event got triggered as well so the card is now expanded too)这是单击图标的时间(您可以看到模式已打开,但也触发了父单击事件,因此卡片现在也已展开)
I thought of a few obvious workarounds ...我想到了一些明显的解决方法......
1 - Giving the user a button on which they can click on expand the card instead of clicking anywhere on the card. 1 - 给用户一个按钮,他们可以点击展开卡片,而不是点击卡片上的任何地方。 (Doesn't fit the requirements)
(不符合要求)
2 - Making the 11 first Col
spaces the click event instead of the whole 12. (Pretty good but if you click on the right side of the card, outside the icon, it doesn't expand the card which is annoying) 2 - 使第 11 个
Col
间隔单击事件而不是整个 12。(很好,但是如果您单击卡片的右侧,在图标之外,它不会展开卡片,这很烦人)
3 - Using the click event's parameter to call preventDefault
like I would do within a JS/JQuery environment but it still triggers the whole thing. 3 - 使用 click 事件的参数来调用
preventDefault
就像我在 JS/JQuery 环境中所做的那样,但它仍然会触发整个事情。
4 - Using local state to store whether or not I'm clicking on the icon currently and if so, prevent it from collapsing the card. 4 - 使用本地 state 存储我当前是否单击图标,如果是,请防止它折叠卡。 (Isn't that a hack around the issue?)
(这不是解决这个问题吗?)
...but they all seem hacks around my real issue which must have a different approach that solves it. ...但他们似乎都围绕着我的真正问题进行黑客攻击,必须有一种不同的方法来解决它。
Any clue?有什么线索吗? I feel I'm missing something in my React understanding as this is something that we must encounter pretty often.
我觉得我在对 React 的理解中遗漏了一些东西,因为这是我们必须经常遇到的东西。
If you want to stop the event bubbling, this can be achieved by the use of the e.stopPropagation()
method, either instead of or alongside e.preventDefault()
.如果你想停止事件冒泡,这可以通过使用
e.stopPropagation()
方法来代替 e.preventDefault() 或与e.preventDefault()
一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.