I am trying to understand the below piece of code
this.handleFilterTextChange = this.handleFilterTextChange.bind(this);
in the snippet below (taken from Reacts official site)
class SearchBar extends React.Component {
constructor(props) {
super(props);
this.handleFilterTextChange = this.handleFilterTextChange.bind(this);
}
handleFilterTextChange(e) {
this.props.onFilterTextChange(e.target.value); // props will give undefined unless we bind this method in the constructor.
}
render() {
return (
<form>
<input
type="text"
placeholder="Search..."
value={this.props.filterText}
onChange={this.handleFilterTextChange}
/>
</form>
);
}
}
I am aware of the bind() upto some extend, this is not a question of usage of the bind(). I am confused because this.handleFilterTextChange is already in the context and why is there a need to bind it again to 'this', why does the this.props not accessible outside the constructor unless the method it is contained in is not bound to this?