ReactJS中{this.handleCreateNote}和this.handleCreateNote()之间的差异

ReactJS中{this.handleCreateNote}和this.handleCreateNote()之间的差异,reactjs,Reactjs,我想知道{this.handleCreateNote}和this.handleCreateNote() 代码 { log(`Pressed keyCode${ev.key}`); 如果(ev.key==“输入”){ 控制台日志(“adf”); this.handleCreateNote();-->这里它不调用this.handleCreateNote; ev.preventDefault(); } }} /> 我想我终于明白了困惑 在{this.handleCreateNote}中,您正在传递一

我想知道
{this.handleCreateNote}
this.handleCreateNote()

代码

{
log(`Pressed keyCode${ev.key}`);
如果(ev.key==“输入”){
控制台日志(“adf”);
this.handleCreateNote();-->这里它不调用this.handleCreateNote;
ev.preventDefault();
}
}}
/>

我想我终于明白了困惑

{this.handleCreateNote}
中,您正在传递一个函数,然后该函数将由事件处理程序调用。您不是在调用该函数,而是在调用它

this.handleCreateNote()
中,您正在自己调用该函数。这就是为什么
()
必须在那里。调用函数时,总是需要括号

比较:

const myFunction = this.handleCreateNote; // we are saving the reference to a function
myFunction(); // we are calling that function
这和

this.handleCreateNote();

但是,当使用处理程序时,您只执行第一步。第二步由事件处理程序完成。

请定义“不工作”。@Sulthan它不调用
handleCreateNote
它是否将“adf”打印到控制台?@Sulthan Yep……那么我很确定在追加
()
时应该调用该函数。你可能在某个地方有潜在的车祸。只需在那里设置一个断点,然后逐步完成代码。
const myFunction = this.handleCreateNote; // we are saving the reference to a function
myFunction(); // we are calling that function
this.handleCreateNote();