Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save erikthedeveloper/fc10894e390e4b6bbb21ee6fbcd8633f to your computer and use it in GitHub Desktop.

Select an option

Save erikthedeveloper/fc10894e390e4b6bbb21ee6fbcd8633f to your computer and use it in GitHub Desktop.
Just a little Form w/ React and Controlled Components!
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')
)
@erikthedeveloper
Copy link
Author

erikthedeveloper commented Apr 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment