Like <field/> <form/> are a helper component you can use to save time. It is tiny wrapper around <form onsubmit={context.formik.handlesubmit}/>. This means you don ' t need to explictly type out <form onsubmit={props.handlesubmit}/> If you don ' t want to.
Withformik (Options)Create a Higher-order React component class that passes props and form handlers (the "Formikbag") into your component Deri Ved from supplied options.
Optionsdisplayname?: stringWhen your inner form component are a stateless functional component, you can use the DisplayName option to give the Compone NT a proper name so you can more easily find it in React DevTools. If specified, your wrapped form would show up as Formik (DisplayName). If omitted, it'll show up as Formik (Component). This option isn't required for class components (e.g. class XXXXX extends React.component {..}).
Enablereinitialize?: BooleanDefault is false. Control whether Formik should reset the form if the wrapped component props change (using a deep equality).
Handlesubmit: (values:values, formikbag:formikbag) = voidYour form submission handler. It is passed your forms values and the ' Formikbag ', which includes an object containing a subset of the injected props and Methods (i.e. all, methods with names, start with set<thing> + Resetform), and any props, were passed to The wrapped component.
The "Formikbag":
Props (props passed to the wrapped component)
Resetform
Seterrors
Setfielderror
Setfieldtouched
SetFieldValue
SetStatus
Setsubmitting
Settouched
Setvalues
Note:errors, touched, status and all event handlers is not included in the formikbag.
Isinitialvalid?: Boolean | (Props:props) = BooleanDefault is false. Control the initial value of IsValid prop prior to mount. You can also pass a function. Useful for situations if you want to enable/disable a submit and reset buttons on initial mount.
Mappropstovalues?: (props:props) = ValuesIf This option was specified, then formik would transfer its results to updatable form state and make these values Availab Le to the new component as props.values. If Mappropstovalues is isn't specified, then formik'll map all props that's not functions to the inner component ' s props . values. That's, if you omit it, Formik would only pass props where typeof props[k]!== ' function ', where k is some key.
Even if your form is not receiving any props from its parent, use Mappropstovalues to initialize your forms empty state.
Validate: (values:values, props:props) = Formikerrors<values> | Promise<any>Note:i suggest using Validationschema and Yup for validation. However, validate is a dependency-free, straightforward-to validate your forms.
Validate the form ' s values with function. This function can either is:
Synchronous and return an errors object.// Synchronous validationconst validate = (values, props) => { let errors = {}; if (!values.email) { errors.email = ‘Required‘; } else if (!/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) { errors.email = ‘Invalid email address‘; } //... return errors;};
Asynchronous and return a Promise that ' s error is an Errors object
// Async Validationconst sleep = ms => new Promise(resolve => setTimeout(resolve, ms));const validate = (values, props) => { return sleep(2000).then(() => { let errors = {}; if ([‘admin‘, ‘null‘, ‘god‘].includes(values.username)) { errors.username = ‘Nice try‘; } // ... if (Object.keys(errors).length) { throw errors; } });};
Validateonblur?: BooleanDefault is true. Use the This option to the run validations on Blur events. More specifically, when either Handleblur, setfieldtouched, or settouched is called.
Validateonchange?: BooleanDefault is true. Use the This option to the tell Formik to run validations on the change events and change-related methods. More specifically, when either HandleChange, SetFieldValue, or setvalues is called.
Validationschema?: Schema | ((props:props) = Schema)A Yup schema or a function that returns a Yup schema. This was used for validation. Errors is mapped by key to the inner component ' s Errors. Its keys should match those of values.
Injected props and methodsThese is identical to the props of <formik render={props = ...}/>
Using Connect () in FormikConnect () is a high-order component, that is injects raw formik context as prop called Formik into the inner component. Fun Fact:formik uses connect () under the hood to wire up <field/>, <fastfield>, and <form>. Advanced users could find it useful to use connect () when building custom components.
import { connect } from ‘formik‘;const SubmitCount = ({ formik }) => <div>{formik.submitCount}</div>;export default connect(SubmitCount);
Easily develop higher-quality react forms with Formik (iv) several other API parsing