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