I am trying to implement a delete functionality in my app, but when I click on the delete button, the item does get deleted but the page is not rerendered right away. I still have to manually refresh the page to see the updated list of items. Is there a way to automatically rerender the page as soon as I hit the delete button? Here is my code:
import React, {useState, useEffect, useRef} from 'react';
import Header from './Header';
import Footer from './Footer';
import Note from './Note';
import CreateArea from './CreateArea';
import axios from 'axios';
function App() {
const [notes, setNotes] = useState([]);
useEffect(() => {
axios.get('http://localhost:4000/').then((response) => {
setNotes(response.data);
});
console.log(notes);
}, []);
const deleteItem = (id) => {
axios
.delete('http://localhost:4000/delete/' + id)
.then(() => {
setNotes((previousValue) => {
return previousValue.filter(({_id}) => {
return _id !== id;
});
});
});
};
return (
<div>
<Header />
<CreateArea />
{notes.map((item, index) => (
<Note
key={item._id}
id={item._id}
noteTitle={item.title}
noteContent={item.content}
onDelete={deleteItem}
/>
))}
<Footer />
</div>
);
}
export default App;