【发布时间】:2020-02-07 01:31:51
【问题描述】:
我希望每个笔记都有一个删除按钮,单击该按钮会在我的状态下从数组中删除该笔记,但 .splice 删除的是最后一个元素而不是索引。
我添加了一条警告语句来验证索引是否正确。警报显示正确的数字,但拼接删除了最后一个元素。为什么会这样?
constructor(props) {
super(props);
this.state = {
Notes: ["1","2","3","4"]
}
}
addNote = () => {
var noteList = [...this.state.Notes];
var newNote = "";
this.setState({ Notes: noteList.concat(newNote) });
}
deleteNote = (index) => {
var noteList = [...this.state.Notes];
alert(index);
noteList.splice(index, 1);
this.setState({ Notes: noteList });
}
renderNotes(Notes) {
return (
<div>
{Notes.map((Note, index) =>
<div class="note">
<div class="noteTop">
<button id="menu"><FontAwesomeIcon icon={faEllipsisV} /></button>
<button id="delete" onClick={() => this.deleteNote(index)}><FontAwesomeIcon icon={faTimes} /></button>
</div>
<textarea class="noteMain">{Note}</textarea>
</div>
)}
</div>
);
}
【问题讨论】:
-
你在哪里打电话
renderNotes()?
标签: reactjs