Created
October 21, 2018 18:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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