Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save takahirohonda/c209fc02486132fa6338db2cfb5e8e65 to your computer and use it in GitHub Desktop.
Save takahirohonda/c209fc02486132fa6338db2cfb5e8e65 to your computer and use it in GitHub Desktop.
refactoring-react-es6-class-components-with-property-initialisers-1
class Form extends ReactComponents {
constructor(props) {
super(props)
this.state = {id: ''}
this.submitHandler = this.submitHandler.bind(this)
this.nameInputHandler = this.nameInputHandler.bind(this)
this.idInputHandler = this.idInputHandler.bind(this)
}
submitHandler() {
// do more things here ....
// then dispatch action
this.props.onSubmitHandler()
}
nameInputHandler(e) {
// to dispatch action (redux)
this.props.onNameChangeHandler(e.target.value)
}
idInputHandler(e) {
// update local state
this.setState({id: e.target.value})
}
render() {
return (
<form>
<label for="firstname">First name</label>
<input
type="text" name="firstname" id="firstname"
onChange={e => inputHandler(e)}
/>
<label for="id">Id</label>
<input
type="text" name="firstname" id="firstname"
onChange={e => inputHandler(e)}
/>
<button type="submit" onSubmit={() => this.submitHandler()}>
Submit
</button>
</form>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment