Last active
August 31, 2021 14:21
-
-
Save jonathangiardino/75455ff1ba8009a6b73982f344a9c678 to your computer and use it in GitHub Desktop.
Form handling hook for React
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 { 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