Created
April 6, 2019 01:23
-
-
Save mannieschumpert/9b3c081fe628901436d6b4612117a371 to your computer and use it in GitHub Desktop.
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 React, { createContext, useState } from 'react'; | |
export const FormContext = createContext(); | |
const Form = ({ handleSubmit, children, submitButtonText }) => { | |
const [values, setValues] = useState({}); | |
const setValue = ({ name, value }) => { | |
setValues(values => ({ | |
...values, | |
[name]: value, | |
})); | |
}; | |
return ( | |
<FormContext.Provider value={{ values, setValue }}> | |
<form | |
onSubmit={e => { | |
e.preventDefault(); | |
handleSubmit(values); | |
}} | |
> | |
{children} | |
<button type="submit">{submitButtonText}</button> | |
</form> | |
</FormContext.Provider> | |
); | |
}; | |
export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment