Skip to content

Instantly share code, notes, and snippets.

@prodriguezr
Last active February 13, 2023 18:26
Show Gist options
  • Select an option

  • Save prodriguezr/a463f53d66610f36d6b3f1e46294238d to your computer and use it in GitHub Desktop.

Select an option

Save prodriguezr/a463f53d66610f36d6b3f1e46294238d to your computer and use it in GitHub Desktop.
Custom Hook for reactjs javascript project
/* How to implement?:
const initialForm = {
name: '',
email: '',
age: 0,
}
const [ formValues, handleInputChange, reset ] = useForm(initialForm);
*/
import { useState } from 'react';
export const useForm = (initialState = {}) => {
const [values, setValues] = useState(initialState);
const reset = () => {
setValues(initialState);
};
const handleInputChange = ({ target: { name, value } }) => {
console.log(`handleInputChange: ${value}`);
setValues({
...values,
[name]: value,
});
};
return [values, handleInputChange, reset];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment