[英]onClick on li element are not passing target id properly?
I am working on a ReactJS project using MaterialUI. 我正在使用MaterialUI进行ReactJS项目。 I have following code 我有以下代码
{ this.state.categories.map((category, key) => (
<List>
<ListItem button id={key} onClick={this.handleClick}>
<ListItemText primary={category.name} />
{(category.id === Number(this.state.nestedOpenNum)) ?
<ExpandLess /> : <ExpandMore />}
</ListItem>
</List>
)
)}
and state looks something like 状态看起来像
this.state = {
open: false,
nestedOpen: false,
nestedOpenNum: 0,
result: [],
categories: [{
id: 0,
name: "COMPUER SCIENCE",
}, {
id: 1,
name: "BUSINESS",
}, {
id: 2,
name: "ART & HUMANITIES",
}, {
id: 3,
name: "MATH & LOGIC",
},{
id: 4,
name: "SCIENCE",
}]
};
this is the handleClick function- 这是handleClick函数-
handleClick = (e) => {
this.setState({ nestedOpenNum: e.target.id });
this.setState({ nestedOpen: !this.state.nestedOpen });
console.log(e.target.id );
}; };
PROBLEM 问题
When I click in padding area of li element(green area), everything working perfectly. 当我单击li元素的填充区域(绿色区域)时,一切正常。 But e.target.id is not passing when I click on li element(blue area). 但是,当我单击li element(蓝色区域)时,e.target.id没有通过。
how can I solve this? 我该如何解决?
尝试将onClick处理程序作为道具传递给ListItemText组件,在我看来,该组件正在阻止其他元素检测其onClick处理程序。
I believe you should use currentTarget
not target
. 我相信您应该使用currentTarget
而不是target
。 Look at this https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11508#issuecomment-256045682 看看这个https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11508#issuecomment-256045682
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.