Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.