如何提取“ =>”到功能模型

Hum*_*ode 3 reactjs react-native

我在这里比较新鲜,尝试开始对函数使用=>,并且在此代码中无法完全理解函数的内部。它确实有效,是我自己编写的,但只是想完全了解其方式和原因。

还有其他形式可以用不同的“形状”编写完全相同的代码吗?

我了解第一个函数如何提取为:

function deleteNote(NoteID) {
 ...
}
Run Code Online (Sandbox Code Playgroud)

但是无法弄清楚内部是如何工作的。

const deleteNote = noteID => {
        setNote(existingNotes => { return existingNotes.filter((note) => note.id !== noteID);
        })
}
Run Code Online (Sandbox Code Playgroud)

结果很好,只想弄清楚发生了什么... :)

Eld*_*ira 5

简而言之,这些都可以翻译成:

const deleteNote = function(noteId) {
  setNote(function(existingNotes) {
    return existingNotes.filter(function(note) { 
      return note.id !== noteID;
    });
  });
};
Run Code Online (Sandbox Code Playgroud)

如果将零件=>用大括号括起来{},则需要使用return语句返回内容,例如:

setNote(existingNotes => {
  return ...;
})
Run Code Online (Sandbox Code Playgroud)

如果不将其用大括号括起来,则箭头后面的内容将返回,例如:

 existingNotes.filter((note) => note.id !== noteID);
Run Code Online (Sandbox Code Playgroud)

看一下这篇简短的文章