简体   繁体   English

如何防止触发父事件

[英]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.

相关问题 如何使用 jQuery 防止事件在特定父级的整个 DOM 树中被触发 - How to prevent events from being triggered down the entire DOM tree of a specific parent using jQuery 如何防止重复触发自定义事件–或可以“重置” jQuery .one()函数? - How can I prevent a custom event from being triggered repeatedly – or can I “reset” the jQuery .one() function? 如何解决两次触发点击事件的麻烦? - How can I trouble shoot click events being triggered twice? 如何阻止在子元素上触发事件而不是添加指针事件? - How can I stop an event from being triggered on a child element rather than adding pointer events none? 如何阻止windows.onbeforeunload被IE中的javascript:href链接触发? - How can I prevent window.onbeforeunload from being triggered by javascript: href links in IE? 如何防止在不使用 preventDefault() 的情况下执行作为按钮父级的锚标记的导航? - how can I prevent the navigation of the anchor tag that is a parent of a button from being executed without using preventDefault()? JavaScript,我可以不触发onmouseup事件,用我自己的事件代替吗? - In JavaScript, can I prevent the onmouseup event from being triggered, replacing it with my own event? 如何防止其他情况每次触发 - How to prevent else-statement from being triggered everytime 如何防止onmouseleave事件在浏览器外触发? - How to prevent the onmouseleave event from being triggered outside the browser? parent div中的onclick和另一个div中参数化的function同时被触发。 我如何防止父母解雇? - The onclick in the parent div and the parameterized function in the other div are triggered at the same time. How do I prevent the parent from firing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM