Last active
December 28, 2018 02:02
-
-
Save Sergioamjr/315ae360d0cb139ecce2597496b38f13 to your computer and use it in GitHub Desktop.
useState example from React Hooks
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, { useState } from "react"; | |
function ComponentWithUseState() { | |
//states independentes | |
const [name, setName] = useState(''); | |
const [age, setAge] = useState(''); | |
//único state com várias propriedades | |
const [address, setAddress] = useState({street: '', city: ''}) | |
const onChangeNameHandler = (event) => { | |
const {target: {value}} = event | |
setName(value) | |
} | |
const onChangeAgeHandler = (event) => { | |
const {target: {value}} = event | |
setAge(value) | |
} | |
const onChangeAddressHandler = (event) => { | |
const {target: {value, name}} = event | |
setAddress({...address, [name]: value}) | |
} | |
return( | |
<form> | |
<input value={name} onChange={onChangeNameHandler} /> | |
<input value={age} onChange={onChangeAgeHandler} /> | |
<input value={address.street} name="street" onChange={onChangeAddressHandler} /> | |
<input value={address.city} name="city" onChange={onChangeAddressHandler} /> | |
</form> | |
) | |
} | |
export default ComponentWithUseState; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment