Skip to content

Instantly share code, notes, and snippets.

@jonathangiardino
Last active August 31, 2021 14:21
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 jonathangiardino/75455ff1ba8009a6b73982f344a9c678 to your computer and use it in GitHub Desktop.
Save jonathangiardino/75455ff1ba8009a6b73982f344a9c678 to your computer and use it in GitHub Desktop.
Form handling hook for React
import { useState } from 'react';
const useForm = (defaults) => {
const [values, setValues] = useState(defaults);
const updateValue = (e) => {
// check if its a number and convert
let { value, type, name } = e.target;
if (type === 'number') {
value = parseInt(value);
}
setValues({
...values,
[name]: value,
});
}
return { values, updateValue };
}
export default useForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment