Last active
March 8, 2017 16:33
-
-
Save tgoyer/13f708690f10a84ea1c8fda642f1d601 to your computer and use it in GitHub Desktop.
React Form Component example
This file contains 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
class FormContainer extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
formData: null | |
} | |
} | |
formSubmit = (evt) => { | |
const form = evt.target, | |
data = { | |
name: form.name.value, | |
email: form.email.value | |
}; | |
this.setState({ formData: data }); | |
evt.preventDefault(); | |
} | |
render() { | |
return ( | |
<div> | |
<pre>{ JSON.stringify(this.state.formData) }</pre> | |
<Form onSubmit={ this.formSubmit }/> | |
</div> | |
) | |
} | |
} | |
const Form = (props) => { | |
return ( | |
<form onSubmit={ this.props.onSubmit }> | |
<TextInput name="name" /> | |
<TextInput name="email" /> | |
<SubmitInput value="submit" /> | |
</form> | |
) | |
} | |
const TextInput = (props) => { | |
return ( | |
<div> | |
<label htmlFor={ this.props.name }>{ this.props.name }</label> | |
<input name={ this.props.name } type={ this.props.type } /> | |
</div> | |
) | |
} | |
const SubmitInput = (props) => { | |
return ( | |
<input value={ this.props.value } type="submit" /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment