Created
October 9, 2018 00:22
-
-
Save prof3ssorSt3v3/45a39ceb36c490eb4a51759bd79392b2 to your computer and use it in GitHub Desktop.
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
import React, {Component, Fragment} from 'react'; | |
export default class MyComponent extends Component{ | |
constructor(props){ | |
super(props); | |
//initial state values | |
this.state = { | |
name: props.name, | |
domain: props.domain | |
} | |
} | |
submit = (ev) =>{ | |
ev.preventDefault(); | |
//send the data to the server.. or whatever | |
//the data is up to date inside of "this.state" | |
} | |
click = (ev) => { | |
//this code runs before the form onSubmit event | |
//we can run other tasks | |
} | |
updateName = (ev) => { | |
let name = ev.target.value; | |
//do data validation | |
this.setState({name:name}); | |
} | |
updateDomain = (ev) => { | |
let domain = ev.target.value; | |
//do data validation | |
this.setState({domain}); | |
} | |
render(){ | |
return ( | |
<Fragment> | |
<h1>Main Heading</h1> | |
<h2>Sub Heading</h2> | |
<form onSubmit={this.submit}> | |
<p> | |
<label>Username</label> | |
<input type="text" value={this.state.name} | |
onChange={this.updateName} /> | |
</p> | |
<p> | |
<label>Domain</label> | |
<input type="text" value={this.state.domain} | |
onChange={this.updateDomain} /> | |
</p> | |
<p> | |
<button onClick={this.click} >Update</button> | |
</p> | |
</form> | |
</Fragment> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment