Created
January 21, 2019 16:33
-
-
Save beardedtim/deb7e8486f50e5c26ebe72fa5840c22c to your computer and use it in GitHub Desktop.
A basic form in React
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 } from 'react' | |
| class Form extends Component { | |
| constructor(props) { | |
| super(props) | |
| this.onSubmit = this.onSubmit.bind(this) | |
| this.onChange = this.onChange.bind(this) | |
| this.state = {} | |
| } | |
| onSubmit(e) { | |
| e.preventDefault() | |
| console.log(this.state) | |
| } | |
| onChange(e) { | |
| const { name, value } = e.target | |
| this.setState(() => ({ [name]: value })) | |
| } | |
| render() { | |
| return ( | |
| <form onSubmit={this.onSubmit}> | |
| <input type="text" name="title" onChange={this.onChange} /> | |
| <input type="submit" value="Submit Form" /> | |
| </form> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment