Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { withFormik } from 'formik';
- const MyForm = props => {
- const {
- values,
- touched,
- errors,
- handleChange,
- handleBlur,
- handleSubmit,
- } = props;
- return (
- <form onSubmit={handleSubmit}>
- <input
- type="text"
- onChange={handleChange}
- onBlur={handleBlur}
- value={values.name}
- name="name"
- />
- {errors.name && touched.name && <div id="feedback">{errors.name}</div>}
- <button type="submit">Submit</button>
- </form>
- );
- };
- const MyEnhancedForm = withFormik({
- mapPropsToValues: () => ({ name: '' }),
- // Custom sync validation
- validate: values => {
- const errors = {};
- if (!values.name) {
- errors.name = 'Required';
- }
- return errors;
- },
- handleSubmit: (values, { setSubmitting }) => {
- setTimeout(() => {
- alert(JSON.stringify(values, null, 2));
- setSubmitting(false);
- }, 1000);
- },
- displayName: 'BasicForm',
- })(MyForm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement