I am currently working on a todo list and I want to be able to delete data stored in an array using the filter method. However, I am encountering an error with my code. Here is the snippet:
const onDelete = (id) => {
setaddItem((prevData) => {
console.log(prevData);
});
}
I am trying to implement it like this:
const onDelete = (id) =>{
setaddItem((prevData) =>{
prevData.filter((value, index) =>{
return index !== id;
})
})}
I'm not sure why I am getting this error. Can someone please help me troubleshoot? Below you can find the complete code for my todo list:
function App() {
const [addItem, setaddItem] = useState([]);
const addNote = (note) => {
setaddItem((prevData) => {
console.log(prevData);
return [...prevData, note];
});
};
const onDelete = (id) => {
setaddItem((prevData) => {
console.log(prevData);
});
}
return (
<>
<Header />
<CreateNote passNote={addNote} />
{
addItem.map((val, index) => {
return (
<Note
key={index}
id={index}
title={val.title}
content={val.content}
deleteItem={onDelete}
/>
);
})}
<Footer />
</>
);
}
export default App;
I am facing an issue with my todo list where the 'addItem.map' function is not being recognized. Any guidance on how to resolve this would be greatly appreciated. Thank you!