我的 react 类中有渲染方法。循环数据,我附加 onClick 事件。但是,当调用 onClick 时,它始终附加列表中的最后一次。 这是我的代码。
for (let item in summaryData) {
var section = summaryData[item];
for (var i = 0; i < section.length; i++) {
var identity = (section[i].name).split('|');
items.push(
<tr key={`team-${item}-row-${i}`}>
<th scope="row">{item}</th>
<td><a href="#" onClick={() => this.handleShowModal(identity[0], identity[1], 'inactive')}>
{section[i].myData.myList}
</a></td>
);}}
当我调用handleShowModal时,它总是显示identity[0]和identity[1]列表中的最后一条记录
这与闭包有关,当我尝试将 items.push 包装到 (function(j){....})(i) 中时,它无法识别 handleShowModal 函数。
谁能建议如何处理这个问题?
最佳答案
试试这个;
<td><a href="#" onClick={this.handleShowModal.bind(this, identity[0], identity[1], 'inactive')}>
关于javascript - Reactjs 和闭包问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621744/