Skip to content

Instantly share code, notes, and snippets.

@damiancolaneri
Created January 31, 2021 18:11
Show Gist options
  • Save damiancolaneri/07dc2c50f72c7de0071f6aa76b060da6 to your computer and use it in GitHub Desktop.
Save damiancolaneri/07dc2c50f72c7de0071f6aa76b060da6 to your computer and use it in GitHub Desktop.
Custom Hook para manejo de formularios
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