Why we have to bind this while calling the function?

Tell us what’s happening:
I saw it from the get a hint section while exercising by myself binding this while calling the function is illogical for me, cause we have already had bind this.

Your code so far


<input onChange = {this.handleChange.bind(this)}/>

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Create a Controlled Input

Link to the challenge:

Hi @nuruddinshohan. There is a comprehensive answer to your question in this article.

yeah, I understand why we have to use bind. But in get a hint section of the exercise

bind is used in calling the function, without it code works perfectly and gets accepted too.

There is this statement in the hint.

PS - Here is another way to bind this into a function: <input onChange = {this.handleChange.bind(this)}/>

The above is an alternative to:

this.handleChange = this.handleChange.bind(this)
1 Like