Source:  Twitter logo

In Formik, how to make the Reset button reset the form only after confirmation?

My code below still resets the form even when you click Cancel.

var handleReset = (values, formProps) => {
    return window.confirm('Reset?'); // still resets after you Cancel :(
};

return (
  <Formik onReset={handleReset}>
    {(formProps) => { 
      return (
        <Form>
          ...
          <button type='reset'>Reset</button>
        </Form>
      )}}
  </Formik>
);

Hello @Aximili you can use resetForm in onSubmit.

onSubmit={(values, { resetForm }) => {

      // do your stuff 
      resetForm();

}}

what resetForm can do?

Imperatively reset the form. This will clear errors and touched, set isSubmitting to false, isValidating to false, and rerun mapPropsToValues with the current WrappedComponent's props or what's passed as an argument. The latter is useful for calling resetForm within componentWillReceiveProps.

95 users liked answer #0dislike answer #095
patelarpan profile pic
patelarpan

I'm not completely certain, but I think you will have to write your own reset function without a button with a reset type. Something like this:

const handleReset = (resetForm) => {
  if (window.confirm('Reset?')) {
    resetForm();
  }
};

function Example() {
  return (
    <Formik initialValues={{ value: 1 }}>
      {formProps => {
        return (
          <Form>
            <Field name="value" type="number" />
            <button
              onClick={handleReset.bind(null, formProps.resetForm)}
              type="button"
            >
              Reset
            </button>
          </Form>
        );
      }}
    </Formik>
  );
}

If you really want to use onReset, I think the only way is to throw an error. The Formik source code seems to indicate resetForm() will be called no matter what your onReset() function returns.

const handleReset = () => {
  if (!window.confirm('Reset?')) {
    throw new Error('Cancel reset');
  }
};

function Example() {
  return (
    <Formik initialValues={{ value: 1 }} onReset={handleReset}>
      {formProps => {
        return (
          <Form>
            <Field name="value" type="number" />
            <button type="reset">
              Reset
            </button>
          </Form>
        );
      }}
    </Formik>
  );
}

I would still go with the first solution though personally.

11 users liked answer #1dislike answer #111
Ciarán Tobin profile pic
Ciarán Tobin

I understand OP's question was for Formik.

However, for anyone using useFormik hook and wanting to clear form on 'reset' button click, here's one way to achieve that.

<button type="reset" onClick={ e => formik.resetForm()}> Reset</button>

7 users liked answer #2dislike answer #27
Anil_M profile pic
Anil_M

My issue was I did not wanted to use window.confirm(). I wanted to use my own modal so I used

useFormikContext()

You can create a function component like this

let resetPresForm = {};// You will have to define this before useEffect

const ResettingForm = () => {
    const { resetForm } = useFormikContext();
    resetPresForm = resetForm; // Store the value of resetForm in this variable
    return null;
};

Now Import this function component inside your

<Formik>
     <button type="button" onClick={() => { setShowResetModal(true) }} className="comman-btn">Reset</button>
     <ResettingForm />
</Formik>

Now on modal component confirm you can

const resetPrescriptionForm = () => {
    setShowResetModal(false);
    resetPresForm();
}

Reference

1 users liked answer #3dislike answer #31
ayu.sheee profile pic
ayu.sheee

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).