Skip to content

Instantly share code, notes, and snippets.

@MorenoMdz
Created April 28, 2018 21:18
Show Gist options
  • Save MorenoMdz/13636cfc0673947e7393d520e1875eb4 to your computer and use it in GitHub Desktop.
Save MorenoMdz/13636cfc0673947e7393d520e1875eb4 to your computer and use it in GitHub Desktop.
/* Form on submit */
<form onSubmit={(e) => {
e.preventDefault();
const data = [...this.state.data,
this.state.inputText];
this.setState({data, inputText: ''});
}}>
<input
type='text'
name='inputText'
value={this.state.inputText}
onChange={(e) => {
this.setState({[e.target.name]: e.target.value})
}}
/>
</form>
/**
* On form submit takes the event and prevent default behaviour
* then create a new array at the end of ...this.state.data with the value from the input
* then setState passing the new arra 'data' and re set inputText to empty.
* JSX displays the name and the vale of the event target.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment