简体   繁体   中英

How can I use formik's setSubmitting() method on onSubmit handler?

I try to call formik's setSubmitting method inside the onSubmit handler . First I create a ref for formik like this :

const formikRef = React.createRef();

Then I try to call it in onSubmit handler :

function Add() {
    const formikRef = React.createRef();
    const onSubmit = (e) => {
        console.log(e);
        setTimeout(() => {
            formikRef.setSubmitting(false);
        }, 4000);
    };

    return (
        <Formik
            innerRef={formikRef}
            initialValues={initialValues}
            onSubmit={onSubmit}
            validationSchema={validationSchema}
        >
            {(props) => {
                return (
                    <Form>
                        ...
                    </Form>
                );
            }}
        </Formik>
    );
}

export default Add;

But it throws an error like this :

TypeError: formikRef.setSubmitting is not a function

How can I use formik's setSubmitting() onSubmit function with or without formik's innerRef ?

ref 's values in react are inside a property called current . the correct way would be:

formikRef.current.setSubmitting(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM