I have a bit of an understanding on how bind works, but in my component I'm using this
alot and I'm a bit confused on what .bind(this) is doing and how it interacts with the onClick and onChange. I know that that this.addTodo
is referring to the TodoInput
class
class TodoInput extends Component {
constructor(props) {
super(props);
this.state = {
value: 'test',
};
this.handleChange = this.handleChange.bind(this);
this.addTodo = this.addTodo.bind(this);
}
handleChange(e) {
console.log('CHANGE HERE');
}
addTodo(todo) {
console.log('TODO:', todo);
}
render() {
return (
<div>
<input type="text" value="" onChange={this.handleChange} />
<button className="btn btn-primary" onClick={() => this.addTodo(this.state.value)}>Submit</button>
</div>
);
}
}