DEV Community

samairali
samairali

Posted on

Two type of functions and there difference

I have one scenario where i am submiting the form data and alert the data , i am working on react js so the thing is on submit the code is
onSubmit={ this.handlesubmit }
and here comes the issue which i dont understand
when i declare function like this

handlesubmit(e){
e.preventDefault()
alert( ${this.state.inpUname} ${this.state.inpComment} ${this.state.profession} )
}

it gives error , but when i declare function like that everything is ok

handlesubmit = (e) => {
e.preventDefault()
alert( ${this.state.inpUname} ${this.state.inpComment} ${this.state.profession} )
}

i want to know the difference

Top comments (1)

Collapse
 
dance2die profile image
Sung M. Kim

I've recently answered a question for the differences in length on StackOverflow.

stackoverflow.com/a/57327565/4035

The answer you are looking for is the section labeled, Following up with the comment. but you might want to read the whole answer.

The gist is due to the arrow function syntax not creating this of its own.