Disabling Submit button on click with useRef (to prevent double clicks) inside a Formik form causes Formik submission to break

gene b.

I have a Formik form as below, and it works OK in its original implementation. One extra feature I had to add is that when the Submit button is clicked, I have to disable it, to prevent double-clicks. I followed the example here which is to define a ref={submitBtnRef} on a button. But after inserting this code in the onClick, Formik doesn't submit anymore, it just stops. If I remove this new code everything works. There are no validation errors; I output Formik's errors and it's empty. Also no console errors.

const submitForm = async (formValues) => {
    const url = '/app/submitAgreement';
    let obj = {'formValues': formValues};
    await axios.post(url, obj);
}

// Define a Submit Button Ref for disabling in Submit's onClick() below
let submitBtnRef = useRef();

<Formik 
    enableReinitialize
    validationSchema={mySchema} 
    onSubmit={ (values) => {
        submitForm(values); // Calls submitForm() function if there are no errors
    }}
    initialValues={initialFormValues}
>
    {
        ({handleSubmit, handleChange, handleBlur, setFieldValue, setFieldTouched, values, touched, isValid, errors}) 
        => (

            <Form onSubmit={handleSubmit}> {/* Form starts here */}
            ..
            ..
            <Button type="submit" ref={submitBtnRef}
                    onClick={() => { 
                       // If I remove this Ref-Disable code it works, but I need to disable Submit
                       if(submitBtnRef.current) {
                          submitBtnRef.current.setAttribute("disabled", "disabled");
                       }
            
                       // The default form-submit method specified in the Formik Form, submitForm(),
                       // will now be executed if there are no validation errors
                       }} 
    >Submit</Button>                
    ..
    </Form>
Ro Milton

Don't manipulate the DOM directly. The link you provided is outdated and has some misleading information. Instead, set attribute disabled in JSX. You can simply use Formik's isSubmitting value:

const submitForm = async (formValues) => {
    const url = '/app/submitAgreement';
    let obj = {'formValues': formValues};
    await axios.post(url, obj);
}

<Formik 
    enableReinitialize
    validationSchema={mySchema} 
    onSubmit={ (values) => {
        submitForm(values); // Calls submitForm() function if there are no errors
    }}
    initialValues={initialFormValues}
>
    {
        ({handleSubmit, handleChange, handleBlur, setFieldValue, setFieldTouched, values, touched, isValid, errors, isSubmitting}) 
        => (

            <Form onSubmit={handleSubmit}> {/* Form starts here */}
            ..
            ..
            <Button type="submit" disabled={isSubmitting}
    >Submit</Button>                
    ..
    </Form>

And if you want the button to stay disabled:

const [isSubmitted, setIsSubmitted] = useState(false);

//...

const submitForm = async (formValues) => {
    const url = '/app/submitAgreement';
    let obj = {'formValues': formValues};
    await axios.post(url, obj);
    setIsSubmitted(true);
}

//...
<Button type="submit" disabled={isSubmitting || isSubmitted}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Bootstrap + Formik - show errors after I click submit button

Formik QRcodeReader button submit for itself

Formik resets form before submit

How to disable formik form after submission of form?

Prevent Formik from clearing form

How to submit Formik when button submit outside tag formik in ReactJs?

Formik clear form on button reset

how to prevent double click event on submit button

Prevent submit on route change Formik AutoSave

Formik React with 2 buttons (Submit and Save) to submit form - Save button not to trigger validation

React Formik bind the external button click with onSubmit function in <Formik>

React Formik - Trigger validation only on form submit

How to reset / empty form after submit in Formik

{React Native} Resetting Formik form after submit

Formik setting errors on submission

Added validations using Formik, but the error is not showing up when I click submit button

React: formik form, how to use state after submit inside a callback function

Disabling double click on winform button

Formik form does not recognize Material ui Button

How to call a method on form submission if there is a validation error using Formik + Yup?

Clear Formik form on submission not working, what am i doing wrong?

How to use useRef with Typescript/Formik?

formik form reset after form submit react js

Yup.mixed().test() seems to break Formik form validation

Intercept a form submit in JavaScript and prevent normal submission

How to prevent submit by double click

Disabling a submit button when clicking on it , will prevent the form from being submitted on Google Chrome

(React, Formik & Yup) Disable or/and change a class of the submit button

reactjs - Formik form does not fire submit on return key press