I have multiple inputs; how can I know which input is changed? I think one way to do it is I can pass additional param to my handleChange function. I know I can do this in jsx onChange={this.handleChange.bind(this, 'name');
But I don't know how to do that since the constructor had bind the function.
class HelloWorldComponent extends React.Component {
constructor(){
super();
this.handleChange = this.handleChange.bind(this);
this.state = {
name:"",
age:""
}
}
render() {
const person = {"name":"james", "age":18};
return (
<div>
<input onChange={this.handleChange} type="text" placeholder="name" defaultValue={person.name} /><br />
<input onChange={this.handleChange} type="text" placeholder="age" defaultValue={person.age} />
</div>
);
}
handleChange(e){
// how to get name and age here?
console.log(e.target.value)
}
}