onSubmit无法在react.js上使用Formik并刷新站点

时间:2019-07-02 20:57:53

标签: reactjs validation formik

我正在Formik中使用React.js项目中的验证表单,但不幸的是它不起作用,当我选择提交按钮孔时,将刷新...

<Formik initialValues = {{
            email: 'test@test.com',
            password: '',
            tags: [],
            date: null,
            state: {value: 'reasonml', label: 'ReasonML'}
        }}
validationSchema = {
    SignupSchema
}
onSubmit = {
    this.handleSubmit
} > {
    ({
        handleSubmit,
        setFieldValue,
        setFieldTouched,
        handleChange,
        handleBlur,
        values,
        errors,
        touched,
        isSubmitting
    }) => (
        <Form >
            <FormGroup className="form-group has-float-label">
                <Label><IntlMessages id="forms.email"/></Label>
                <Field className="form-control" name="email"/> {errors.email && touched.email
                    ? (
                        <div className="invalid-feedback d-block">{errors.email}</div>
                    )
                    : null}
            </FormGroup>
            <FormGroup className="form-group has-float-label">
                <Label><IntlMessages id="forms.password"/></Label>
                <Field className="form-control" name="password" type="password"/> {errors.password && touched.password
                    ? (
                        <div className="invalid-feedback d-block">{errors.password}</div>
                    )
                    : null}
            </FormGroup>
            <FormGroup className="form-group has-float-label">
                <Label className="d-block"><IntlMessages id="form-components.tags"/></Label>
                <FormikTagsInput
                    name="tags"
                    value={values.tags}
                    onChange={setFieldValue}
                    onBlur={setFieldTouched}/> {errors.tags && touched.tags
                    ? (
                        <div className="invalid-feedback d-block">{errors.tags}</div>
                    )
                    : null}
            </FormGroup>
            <FormGroup className="form-group has-float-label">
                <Label className="d-block"><IntlMessages id="form-components.date"/></Label>
                <FormikDatePicker
                    name="date"
                    value={values.date}
                    onChange={setFieldValue}
                    onBlur={setFieldTouched}/> {errors.date && touched.date
                    ? (
                        <div className="invalid-feedback d-block">{errors.date}</div>
                    )
                    : null}
            </FormGroup>
            <FormGroup className="form-group has-float-label">
                <Label><IntlMessages id="forms.state"/></Label>
                <FormikReactSelect
                    name="state"
                    id="state"
                    value={values.state}
                    options={options}
                    onChange={setFieldValue}
                    onBlur={setFieldTouched}/> {errors.state && touched.state
                    ? (
                        <div className="invalid-feedback d-block">{errors.state}</div>
                    )
                    : null}
            </FormGroup>
            <Button color="primary" type="submit">Submit</Button>
        </Form>
    )
}

我使用的模式:


const SignupSchema = Yup.object().shape({
    email: Yup.string()
        .email('Invalid email address')
        .required('Email is required!'),
    password: Yup.string()
        .required('Password is required!'),
    tags: Yup.array()
        .min(3, 'Pick at least 3 tags')
        .required("At least one tag is required"),
    date: Yup.date()
        .nullable()
        .required("Date required"),
    state: Yup.object().shape({
        label: Yup.string().required(),
        value: Yup.string().required(),
    })
        .nullable()
        .required('State is required!'),
})

及其handlesubmit方法:


    handleSubmit = (values, {setSubmitting}) => {

        const payload = {
            ...values,
            state: values.state.value,
        };
        setTimeout(() => {
            console.log(JSON.stringify(payload, null, 2))
            setSubmitting(false);
        }, 1000);
    }

在这种形式的表单中,onSubmit函数不起作用。我不知道为什么?请告诉我我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,原因是我从“reacstrap”(另一个库)而不是从“Formik”导入组件“Form”。