Home > OS >  function is not defined when im trying to call a function inside my onsubmit function
function is not defined when im trying to call a function inside my onsubmit function

Time:11-28

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")
  }

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related