0

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;
  }

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

8
  • Not a solution but remember to add the key parameter for each Note inside the map. I think the problem relies in this.noteId, shouldn't it be this.props.noteId (inside onClick)? Commented Nov 27, 2018 at 0:26
  • should it be this.handleRemove(this .props .noteId)} ? Commented Nov 27, 2018 at 0:30
  • constructor(props){ super(props); this.noteId = props.noteId; this.noteContent = props.noteContent; } Commented Nov 27, 2018 at 0:42
  • constructor of Note component Commented Nov 27, 2018 at 0:43
  • 1
    @Alvaro Well, code links ready Commented Nov 27, 2018 at 2:29

1 Answer 1

1

here's a few problems in the code you provided by the links: 1. the key property must be given when iterating in map

this.state.notes.map(note =>
<Note
removeNote = {this.handleRemoveNote}
noteId = {note.noteId}
noteContent= {note.noteContent}
key={note.noteId}
/>)

and then it should be removed from Note render() method

  1. you don't actually want to re-assign you keys after removal, must probably you will get those ids from the server, so you have to use them as keys. And also it's the main problem here

this way your new notes array won't contain the element with the key which is the index of your last element, so it thinks you want to remove the element with this key - the last element. Just remove that loop and everything should work fine

for (var i = 0; i < notes.length; i++) {
    notes[i].noteId = i+1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

that reorder of keys is because when you delete a note for example I had note 1, note 2 and note 3 if you deleted note 2 and added a new one had two notes with the same key 3
then you might need to use something else as a key for your elements, you can't rely on the ids you will provide from ui, the ids used for keys must be unique, possible solutions: 1) you use some internal ids as keys and they will be unique for all the notes, you can use something like npmjs.com/package/shortid 2) don't enter ids from ui and assign them internally and be sure they are always unique + remove that re-ordering code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.