Skip to content

Instantly share code, notes, and snippets.

@mannieschumpert
Created April 6, 2019 01:23
Show Gist options
  • Save mannieschumpert/9b3c081fe628901436d6b4612117a371 to your computer and use it in GitHub Desktop.
Save mannieschumpert/9b3c081fe628901436d6b4612117a371 to your computer and use it in GitHub Desktop.

Revisions

  1. mannieschumpert created this gist Apr 6, 2019.
    30 changes: 30 additions & 0 deletions Form.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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;