Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dennisja/2388ee8d93bd43f1c5575c778f2e2c40 to your computer and use it in GitHub Desktop.

Select an option

Save dennisja/2388ee8d93bd43f1c5575c778f2e2c40 to your computer and use it in GitHub Desktop.
A simple form to show lessons learned about JS when building a simple controlled form in react without using class properties
import React, { Component, Fragment } from "react";
class TodoForm extends Component {
constructor(props) {
super(props);
this.state = {
todoName: ""
};
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
const { name, value } = event.target;
this.setState({ [name]: value });
}
handleAddToDo(event) {
event.preventDefault();
// do add todo logic here
}
render() {
return (
<form onSubmit={this.handleAddToDo}>
<label htmlFor="todoName">Todo Name: </label>
<input
type="text"
name="todoName"
id="todoName"
placeholder="Name Your TODO"
onChange={this.handleInputChange}
/>{" "}
<input type="submit" value="Add" />
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment