When I click on a delete button, I want to then filter out that ID and re-render the list, but I am getting an annoying error as mentioned in the title.
It will eventually send a request to the server to delete from the database but for now all I want is the item to be removed from the list and the list to show up again without that particular record. It is basically a table with a bunch of records. Each one has a delete button next to it. When you click delete it should remove that item which is what I am trying to do with .filter
and then set the state to the new loadedFaq
minus the record that was filtered out.
The button:
<Button
color="purple"
label="Delete"
onClick={() => deleteHandler(faq)}
/>
The delete handler:
const deleteHandler = async (faq) => {
const originalFaq = loadedFaq; // loadedFaq is the original state
const loadedFaq = originalFaq.filter((f) => f._id !== faq._id);
setLoadedFaq(loadedFaq); // set the state to the original state minus the filtered out record
};
UPDATE:
It doesn't like the fact that I am redeclaring 'loadedFaq'. If I do this it works:
const deleteHandler = async (faq) => {
const originalFaq = loadedFaq;
const loadedFaqTwo = originalFaq.filter((f) => f._id !== faq._id);
setLoadedFaq(loadedFaqTwo);
};