Last active
April 28, 2016 13:17
-
-
Save erikthedeveloper/fc10894e390e4b6bbb21ee6fbcd8633f to your computer and use it in GitHub Desktop.
Just a little Form w/ React and Controlled Components!
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 from 'react'; | |
| const SomeForm = React.createClass({ | |
| getInitialState() { | |
| return {firstName: ''} | |
| }, | |
| handleChangeFirstName(event) { | |
| this.setState({ | |
| firstName: event.target.value | |
| }); | |
| }, | |
| handleSubmit(event) { | |
| event.preventDefault(); | |
| alert(`First Name: ${this.state.firstName}!`); | |
| }, | |
| render() { | |
| const {firstName} = this.state; | |
| const secretMessage = firstName.length > 0 ? `for ${firstName}` : ''; | |
| return ( | |
| <div> | |
| <h3>A Great Form {secretMessage}</h3> | |
| <form onSubmit={this.handleSubmit}> | |
| First Name <input | |
| type="text" | |
| value={firstName} | |
| onChange={this.handleChangeFirstName} | |
| /> | |
| <button>Submit</button> | |
| </form> | |
| </div> | |
| ) | |
| } | |
| }); | |
| ReactDOM.render( | |
| <SomeForm />, | |
| document.getElementById('app') | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Codepen URL: http://codepen.io/erikthedeveloper/pen/PNyprZ?editors=1010