cachaito
6/24/2017 - 11:03 PM

Nice pattern for creating universal onChange event handler for every input (which has to have 'name' attribute)

Nice pattern for creating universal onChange event handler for every input (which has to have 'name' attribute)

class SignupForm extends React.Component {
  constructor(props) {
    super(props);
    
    this.state = {
      username: ''
    };
    
    this._onchange = this.onChange.bind(this);
  }
  
  _onChange(e) {
    this.setState({
      [e.target.name]: e.target.value
    });
  }
  
  render() {
    return(
      <input value={this.state.username} onChange={this._onChange} name="username" />
    )
  }
}