D
D
dotnetlooper2021-12-23 19:26:31
React
dotnetlooper, 2021-12-23 19:26:31

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
61c4a2cdf2384333845349.png

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>


Well, then the delete button itself, but so far there is nothing:
<FontAwesomeIcon 
                        icon={ faTrashAlt }
                        onClick={() => setModal("deleting")} 
                    />

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ParaBellum577, 2021-12-23
@dotnetlooper

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)}
            />

And the function itself is something like this
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 question

Ask a Question

731 491 924 answers to any question