从事件中反应js调用函数

react js call function from event

如何从同一个 class 中的 React 组件调用函数,我正在使用 create-react-app BTW。

Somefunction(){ //function
 return true;
}

handleClick(e){ // this works
 Somefunction(); // but fails here as Somefunction is undefined
}

我想我没有正确绑定它,为什么它未定义?

谢谢

假设您已将 handleClick 函数绑定到 class 实例,您必须使用 this.[=14= 访问实例上的 Somefunction 函数]

Somefunction() { 
  return true;
}

handleClick(e) {
  this.Somefunction();
}