![](/img/trans.png)
[英]Why am I getting "Cannot access 'server' before initialization" error in NodeJS?
[英]I am trying to delete a note in react but I am getting an error that you cannot access new notes before initialization?
React Find error in delete note function that can't access the newNotes before初始化那為什么不能刪除該注釋?
deleteNote function 正在工作,因為它的控制台使用我在其中寫入的 id 記錄消息但它沒有刪除便箋,我已經聲明了 newNote 並在其中初始化了值但我不明白為什么它給出了錯誤?
const [notes, setNotes] = useState(notesInitial)
//Add Note
const addNote = (title, description, tag) => {
//TODO API CALL
console.log("adding a new note");
const note = {
"_id": "63074e71318fac99be7ce65a",
"user": "62ee8f0b86e5c4946d3b75d5",
"title": title,
"description": description,
"tag": tag,
"date": "2022-08-25T10:26:57.324Z",
"__v": 0
};
setNotes(notes.concat(note));
}
//Delete A Note
const deleteNote = (id) => {
console.log("The node delteing with id" + id);
I think I missing something here
const newNotes = newNotes.filter((notes) => { return notes._id !== id })
setNotes(newNotes);
}
return (
<noteContext.Provider value={{ notes, addNote, deleteNote, editNote }}>
{props.children}
</noteContext.Provider>
)
}
export default NoteState;
const newNotes = newNotes.filter((notes) => { return notes._id !== id })
newNotes
再次被聲明,它不會從您的useState
中獲取。 在deleteNode
function 中將其命名為其他任何名稱。
const deleteNote = (id) => {
console.log("The node delteing with id" + id);
I think I missing something here
const newNotes2 = notes.filter((notes) => { return notes._id !== id })
setNotes(newNotes2);
}
是的,您在這里犯了錯誤,因此將其轉換為
//Delete A Note
const deleteNote = (id) => {
console.log("The node delteing with id" + id);
I think I missing something here
const newNotes = newNotes.filter((notes) => { return notes._id !== id })
setNotes(newNotes);
}
至
const deleteNote = (id) => {
console.log("The node delteing with id" + id);
I think I missing something here
const newNotes = notes.filter((note) => { return note._id !== id })
setNotes(newNotes);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.