代码之家  ›  专栏  ›  技术社区  ›  fuzzybear

事件中的react js调用函数

  •  2
  • fuzzybear  · 技术社区  · 8 年前

    如何从同一个类中的react组件调用函数,我使用createreact应用程序btw。

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

    我认为我没有正确地绑定它,为什么它没有定义?

    谢谢

    1 回复  |  直到 8 年前
        1
  •  4
  •   Tholle    8 年前

    既然你已经把 handleClick 函数到类实例,必须访问 Somefunction 在实例上使用 this 是的。

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