简体   繁体   English

防止子组件触发父组件的onClick

[英]Prevent child component from triggering parent's onClick

Using Material-ui in React, I want to have the <IconButton> to have one behavior with onClick (quickly add to the cart) and have the parent <GridListTile> to have a different onClick behavior (open the more info Dialog for that item).使用Z7F789E9571FFE88BFCE3D07B862B18777Z在React中,我想让< onClick <IconButton>具有一种行为,具有onClick (迅速添加到购物车)的行为,并添加了333.33.33.33.23.33.2ly <GridListTile> My issue is that when I click on the <IconButton> , it does both actions at the same time (open the more info Dialog AND add to the cart. Is there a way to have <IconButton> to not inherit the onClick from the parent component?我的问题是,当我单击<IconButton>时,它同时执行两个操作(打开更多信息Dialog并添加到购物车。有没有办法让<IconButton>不从父级继承 onClick零件?

<GridListTile key={tile.title}>
              <img
                src={`/${tile.img}`}
                alt={tile.title}
                onClick={handleClickOpen()}
              />
              <GridListTileBar
                title={tile.title}
                subtitle={<span>{tile.description}</span>}
                onClick={handleClickOpen()}
                actionIcon={
                  <Tooltip title="add to cart">
                    <IconButton
                      aria-label={`info about ${tile.title}`}
                      className={classes.icon}
                      // onClick={handleClickOpen()}
                      onClick={() => {
                        dispatch({ type: "cart-increment" });
                      }}
                    >
                      <AddShoppingCartIcon />
                    </IconButton>
                  </Tooltip>
                }
              />
            </GridListTile>

In the child handler, stop the event from propagating upward:在子处理程序中,阻止事件向上传播:

<IconButton
    aria-label={`info about ${tile.title}`}
    className={classes.icon}
    onClick={(e) => {
        e.stopPropagation();
        dispatch({ type: "cart-increment" });
    }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止父事件触发子事件 - Prevent parent event from triggering child events 防止将鼠标悬停在父项上之后子项的onclick吗? - Prevent child's onclick after mouseup on parent? 防止子元素在子菜单项单击时触发父事件 - Prevent child element from triggering parent event on submenu item click 反应如何防止在单击孩子时触发父母的 onclick 事件? - React how to prevent a parent's onclick event from firing when a child is clicked? 单击子锚点时,如何防止父项的 onclick 事件触发? - How do I prevent a parent's onclick event from firing when a child anchor is clicked? 防止在javascript中触发父级的同级事件 - Prevent parent's sibling's event from triggering in javascript 从其子项触发 React 父功能组件中的 function - Triggering a function in a React parent functional component from its child 通过onClick反应将数据从子组件传递到父组件 - React Passing Data From Child Component to Parent Component via a onClick 使用回调和挂钩(useState,useEffect)从父级中的 onClick 事件更改 React 子组件的 state - Change React child component's state from an onClick event in parent, with callbacks and hooks (useState, useEffect) 使用引导程序模式阻止父onclick事件从子元素传播 - Prevent parent onclick event propagation from child element with bootstrap modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM