Skip to content

Instantly share code, notes, and snippets.

@rsturim
Last active June 23, 2020 17:38
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 rsturim/20dde9c75c0767c470ff290fca0da48a to your computer and use it in GitHub Desktop.
Save rsturim/20dde9c75c0767c470ff290fca0da48a to your computer and use it in GitHub Desktop.
import { useState } from 'react'
const useForm = initialValues => {
const [values, setValues] = useState({
...initialValues,
isSubmitting: false,
})
const handleSubmit = (event, callback) => {
if (event) {
event.preventDefault();\
}
setValues(prevValues => ({
...prevValues,
isSubmitting: true,
}))
callback()
setValues(() => ({
...initialValues,
isSubmitting: false,
}))
}
const handleChange = newValues => {
setValues(prevValues => ({
...prevValues,
...newValues,
}))
}
return {
handleChange,
handleSubmit,
values,
}
}
export default useForm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment