Answer the question
In order to leave comments, you need to log in
How to pass an element to remove?
I am making an application that integrates with api. Roughly speaking, the todo-list application.
I'm interested in such a question, there is a component that will be rendered dozens of times and I want to remove some of the elements.
There is a task, to select a component and to delete it. This is where the misunderstanding happens for me, how do I pass the element that I want to delete
const renderNote = (note, index) => {
return (
<Note
key={ note.id }
index={ index }
id={ note.id }
text={ note.text }
date={ note.data }
title = { note.title }
thumbnail={ note.thumbnail }
description={ note.description }
moveNote={ moveNote }
/>
)
};
....
<div>
{
notes && notes.map((note, i) => renderNote(note, i))
}
</div>
<FontAwesomeIcon
icon={ faTrashAlt }
onClick={() => setModal("deleting")}
/>
Answer the question
In order to leave comments, you need to log in
Just pass to Note a function that takes the index of the element, and then remove the desired element from the array by index (Well, or by id, whatever)
<Note
removeElement={() => handleDelete(index)}
/>
const [notes, setNotes] = useState([тут ваши Notes])
const handleDelete = (index) => {
let newNotes = [...notes];
newNotes[index].splice(index, 1);
setNotes(newNotes);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question