Created
November 17, 2017 13:01
-
-
Save joelbarbosa/dae5eb446d4d07360d253450ce156a84 to your computer and use it in GitHub Desktop.
Controlled Component
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 } from 'react'; | |
export default class ControlledComponent extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
name: '', | |
idade: 0, | |
} | |
} | |
handleInput = field => ({ target }) => { | |
this.setState({ [field]: target.value }) | |
} | |
handleSubmit = event => { | |
console.log(this.state) | |
event.preventDefault() | |
} | |
render () { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<input onChange={this.handleInput('name')}/> | |
<input onChange={this.handleInput('idade')} /> | |
<input type="submit" value="Submit" /> | |
</form> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment