What is the usage of Function.prototype.bind?
The bind
method returns a new function that is bound
to a specific this
value or the "owner" object, So we can use it later in our code. The call
,apply
methods invokes the function immediately instead of returning a new function like the bind
method.
import React from 'react';
class MyComponent extends React.Component {
constructor(props){
super(props);
this.state = {
value : ""
}
this.handleChange = this.handleChange.bind(this);
// Binds the "handleChange" method to the "MyComponent" component
}
handleChange(e){
//do something amazing here
}
render(){
return (
<>
<input type={this.props.type}
value={this.state.value}
onChange={this.handleChange}
/>
</>
)
}
}