[英]How to delete the current div in React.js?
我是 React.js 的新手,我想删除在map
函数中生成的当前div
。 当我单击删除按钮时,它会删除数组数组的最后一个元素而不是单击的元素。
这是我的代码:
{
this.state.array.map((item,index)=>{
return(
<div key={index}>
<Question />
<button onClick={() => { this.deleteRow(index); }}>DELETE</button>
</div>
);
})
}
这是我的删除功能
deleteRow = (index) => {
// make new rows. note: react state is immutable.
let newArray = this.state.array;
newArray.pop(newArray[index]);
this.setState({
array: newArray,
});
console.log(this.state.array);
}
<button onClick={() => this.deleteRow(item)}>DELETE</button>
更改此代码
deleteRow = (item) => {
let newArray = this.state.array;
let filtered = newArray.filter(a=>a.id!==item.id);
this.setState({array: filtered});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.