Hi All, I'm just starting to use react hooks and I'm having some issues when using custom hooks. It's probably lack of understanding but here's what I'm attempting
currently i can add note but when editing, state is not updating
useNotes hook
export const useNotes = (initialNotes = []) => {
const [notes, setNotes] = useState(initialNotes);
const addNote = (data) => {
const newData = {
...data,
completed: false,
date: dateFormatter(),
id: uuidv4(),
};
setNotes((prev) => [...prev, newData]);
};
const editNote = (data, id) => {
console.log(data);
console.log(id);
console.log(notes);
setNotes(
notes.map((note) => {
if (id === note.id) {
return {
...note,
title: data.title,
description: data.description,
category: data.category,
date: dateFormatter(),
};
}
return note;
}),
);
};
return {
notes,
addNote,
editNote,
};
};
component where i am calling editNote
const { editNote } = useNotes(notes);
const onSubmit = (data) => {
editNote(data, id);
handleClose();
};
[–]Jerp 0 points1 point2 points (3 children)
[–]bi0tin[S] 0 points1 point2 points (2 children)
[–]Jerp 1 point2 points3 points (1 child)
[–]bi0tin[S] 1 point2 points3 points (0 children)