Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Created November 17, 2017 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelbarbosa/dae5eb446d4d07360d253450ce156a84 to your computer and use it in GitHub Desktop.
Save joelbarbosa/dae5eb446d4d07360d253450ce156a84 to your computer and use it in GitHub Desktop.
Controlled Component
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