Created
January 31, 2021 18:11
-
-
Save damiancolaneri/07dc2c50f72c7de0071f6aa76b060da6 to your computer and use it in GitHub Desktop.
Custom Hook para manejo de formularios
This file contains hidden or 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 { useState } from 'react'; | |
/* | |
Ejemplo de eso del custom hook: | |
const initialForm = { | |
name: "", | |
email: "" | |
}; | |
const [ formValues, handleImputChange, reset ] = useForm(initialForm); | |
*/ | |
export const useForm = (initialState = {}) => { | |
const [values, setValues] = useState(initialState); | |
const reset = (newFormState = initialState) => { | |
setValues(newFormState); | |
}; | |
const handleInputChange = ({ target }) => { | |
setValues({ | |
...values, | |
[target.name]: target.value, | |
}); | |
}; | |
return [values, handleInputChange, reset]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment