简体   繁体   中英

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. what could the reason be

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

You need to call this.validateForm() . validateForm is bound to the component instance.

formSubmit and validateForm are methods of your (component) class, not an independent 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.

You are using this key word in the Component it seems. this key word has varying scope depending on where you are using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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