No working, handleRemoveNote function the App component:
handleRemoveNote(id){
let{ notes } = this.state;
notes = notes.filter(function(note) {
return note.noteId !== id;
})
for (var i = 0; i < notes.length; i++) {
notes[i].noteId = i+1;
}
this.setState({ notes })
console.log(notes)
}
Component Note:
handleRemove(noteId){
this.props.removeNote(noteId);
}
Call onClick
<span
onClick = {() => this.handleRemove(this.noteId)}
>
<i className="fas fa-trash-alt float-right"></i>
</span>
Rendering of the notes in the App component:
{
this.state.notes.map(note =>{
return(
<Note
removeNote = {this.handleRemoveNote}
noteId = {note.noteId}
noteContent= {note.noteContent}
/>
)
})
}
Constructor of Note component:
constructor(props){
super(props);
this.noteId = props.noteId;
this.noteContent = props.noteContent;
}
- App Component: https://pastebin.com/VCrJBdXT
- Note Component: https://pastebin.com/hDmPaGuZ
I do not understand why it does not work, it removes the last item from the list and not the one I want, I made a 'console.log (notes)' and it shows me the arrangement with the elements deleted correctly
this.noteId, shouldn't it bethis.props.noteId(inside onClick)?constructor(props){ super(props); this.noteId = props.noteId; this.noteContent = props.noteContent; }