Skip to content

Instantly share code, notes, and snippets.

@mannieschumpert
Created April 6, 2019 01:23
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 mannieschumpert/9b3c081fe628901436d6b4612117a371 to your computer and use it in GitHub Desktop.
Save mannieschumpert/9b3c081fe628901436d6b4612117a371 to your computer and use it in GitHub Desktop.
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