React : bind()

by Cristi Salcescu

HTML

<div id="root"></div>

React

class TodoAddForm extends React.Component {
  constructor(){
      super();
      this.todos = [];
      //this.handleChange = this.handleChange.bind(this);
      //this.add = this.add.bind(this);
  }
  
  componentWillMount() {
    this.setState({desc: ""});
  }
  
  add(){
    let todo = {desc: this.state.desc};
		this.todos.push(todo);
    console.log(this.todos);
  }
  
  handleChange(event) {
    this.setState({desc: event.target.value});
  }
  
  render() {
    return <form>
      <input onChange={this.handleChange} placeholder="New todo description" value={this.state.desc} type="text"/>
      <button onClick={this.add} type="button">Save</button>
    </form>;
  }
}

ReactDOM.render(
  <TodoAddForm />,
  document.getElementById('root'));