Reactjs 和闭包问题
Reactjs and issue with closures
我的反应中有渲染方法 class。循环数据,我附加了 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')}>
我的反应中有渲染方法 class。循环数据,我附加了 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')}>