Last active
June 23, 2020 17:38
-
-
Save rsturim/20dde9c75c0767c470ff290fca0da48a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = 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