Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset form onSubmit in Formik?

Tags:

reactjs

formik

I have a form built with Formik. And I would like to reset/ empty it after submit. I've tried to add resetForm(), but it's not working. Here is my code:

onSubmit={(values, { setSubmitting, resetForm }) => {
  setTimeout(() => {
    alert(JSON.stringify(values, null, 2));
    setSubmitting(false);
    resetForm();
  }, 400);
}}

And a live example https://codesandbox.io/s/reset-form-8sw24.

Where is my mistake and how can I reset/ empty form onSubmit?

like image 737
jupiteror Avatar asked Feb 03 '26 10:02

jupiteror


1 Answers

I had to add value={values.phone} to the input as I'm using a custom input. After that resetForm() is working.

like image 158
jupiteror Avatar answered Feb 05 '26 02:02

jupiteror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!