REECT JS-点击孩子onclick时,请防止父母onclick
这是我单击行 myparentClick 时的JSX
,但是当我单击任何一个 myChildClick mychildclick2
然后它触发父母和孩子。
首先触发父母,然后是
{myData?.map((item, index) => {
return(
<Row onClick={() => myParentClick(index)}>
<Col>
// some html tags
</Col>
<Col>
// some html tags
</Col>
<Col>
// React Bootstrap DropDown
<DropDown>
<DropDown.Toggle>
// code
</DropDown.Toggle>
<DropDown.Menu>
<DropDown.Item onClick={() => myChildClick(index)}>
<p>some TEXT</p>
</DropDown.Item>
</DropDown.Menu>
</DropDown>
</Col>
<Col onClick={() => myChildClick2(index)} >
<p>some TEXT</p>
</Col>
<Col onClick={() => myChildClick3(index)} >
<p>some TEXT</p>
</Col>
</Row>
)
})}
我尝试过的孩子,在孩子点击e.stoppropagation()上; 在单击孩子onclick时,哪个不起作用
onClick={
(e) => {
e.stopPropagation();
myChildClick(index)
}
}
如何防止父母onclick?
Here is the jsx
When i click the ROW myParentClick is triggered but when i click on any child which is myChildClick myChildClick2
Then it triggers both the parent and child.
The Parent is triggered first and then the child
{myData?.map((item, index) => {
return(
<Row onClick={() => myParentClick(index)}>
<Col>
// some html tags
</Col>
<Col>
// some html tags
</Col>
<Col>
// React Bootstrap DropDown
<DropDown>
<DropDown.Toggle>
// code
</DropDown.Toggle>
<DropDown.Menu>
<DropDown.Item onClick={() => myChildClick(index)}>
<p>some TEXT</p>
</DropDown.Item>
</DropDown.Menu>
</DropDown>
</Col>
<Col onClick={() => myChildClick2(index)} >
<p>some TEXT</p>
</Col>
<Col onClick={() => myChildClick3(index)} >
<p>some TEXT</p>
</Col>
</Row>
)
})}
I have tried, on child click e.stopPropagation(); which is not working
onClick={
(e) => {
e.stopPropagation();
myChildClick(index)
}
}
How to Prevent Parent onClick when clicked on child onClick ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
我拿走了您的代码,并试图复制您的问题。由于某种原因,您的停止propagation对您不起作用,但是当我尝试使用时,它可以正常工作:
https:> https: > https: //stackblitz.com/edit/reeact-ts-mk1dcr
i took your code and tried to reproduce your issue. for some reason your stopPropagation doesnt work for you, but when i tried it, it works fine:
https://stackblitz.com/edit/react-ts-mk1dcr