【发布时间】:2020-07-25 17:21:27
【问题描述】:
我有一个表格,其中的行从数组映射,每行都有一个编辑按钮。我使用 map(value, index) 为每个按钮指定了一个值。
var selected;
handleUpdate = (e) => {
e.persist();
selected = filteredresults[e.target.value];
this.setState({
updateform: true,
viewuser: false,
});
};
<Button
type="button"
color="primary"
value={index}
onClick={(e) => this.handleUpdate(e)} Or onClick={this.handleUpdate}
style={{ width: "5px" }}>
<EditIcon />
</Button>
当我点击按钮时,e.target.value 有时为空或未定义,有时为索引值。
我读到了 react 中的事件池,所以我添加了 e.persist(),但随机行为仍然存在。
为什么是“有时”?预期的行为每次都应该是相同的。
【问题讨论】:
-
尝试注释掉
e.persist,看看会发生什么。 -
我添加了 e.persist 因为它并非每次都有效。有时有时不是。