我在这里使用了反应钩子,但我一直收到这个错误。
React Hook useEffect 缺少一个依赖项:'notes'。包括它或删除依赖数组。如果您只需要在 'setNotes' 调用 react-hooks/exhaustive-deps 中需要 'notes',您也可以进行功能更新 'setNotes(n => ...)'
useEffect(() => {
getNotes();
const createNoteListner = API.graphql(
graphqlOperation(onCreateNote)
).subscribe({
next: noteData => {
const newNote = noteData.value.data.onCreateNote;
setNotes(prevNotes => {
const oldnotes = prevNotes.filter(note => note.id !== newNote.id);
const updatedNotes = [...oldnotes, newNote];
return updatedNotes;
});
setNote("");
}
});
const deleteNoteListner = API.graphql(
graphqlOperation(onDeleteNote)
).subscribe({
next: noteData => {
const deleteNote = noteData.value.data.onDeleteNote;
setNotes(prevNotes => {
const updatedNotes = prevNotes.filter(
note => note.id !== deleteNote.id
);
return updatedNotes;
});
}
});
const updateNoteListner = API.graphql(
graphqlOperation(onUpdateNote)
).subscribe({
next: noteData => {
const updatedNote = noteData.value.data.onUpdateNote;
setNotes(prevNotes => {
const index = prevNotes.findIndex(note => note.id === updateNote.id);
const updatedNotes = [
...notes.slice(0, index),
updatedNote,
...notes.slice(index + 1)
];
return updatedNotes;
});
setNote("");
setId("");
}
});
return () => {
createNoteListner.unsubscribe();
deleteNoteListner.unsubscribe();
updateNoteListner.unsubscribe();
};
}, []);
蝴蝶刀刀
德玛西亚99
相关分类