简体   繁体   English

当我尝试在我的 onsubmit function 中调用 function 时,未定义 function

[英]function is not defined when im trying to call a function inside my onsubmit function

i have this form say我有这个表格说

<form onSubmit={this.formSubmit} id="userForm" name="myForm" noValidate>

im trying to call another function inside my formsubmit() function but it says the function is not defined.我试图在我的 formsubmit() function 中调用另一个 function 但它说 function 未定义。 what could the reason be可能是什么原因

   formSubmit=(e)=>{
    e.preventDefault();
    validateForm();
  }
  validateForm=()=>{
    console.log("test")
  }

You need to call this.validateForm() .您需要调用this.validateForm() validateForm is bound to the component instance. validateForm绑定到组件实例。

formSubmit and validateForm are methods of your (component) class, not an independent function. formSubmitvalidateForm是您的(组件)class 的方法,而不是独立的 function。 In order to call the inside of the class, you need to call it through the this keyword as it is bound to the class.为了调用 class 的内部,您需要通过this关键字调用它,因为它绑定到 class。

You are using this key word in the Component it seems.您似乎在组件中使用this关键字。 this key word has varying scope depending on where you are using it. this关键字有不同的 scope 取决于你在哪里使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM