jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.77k stars 2.78k forks source link

resetForm() doesn't reset form #3769

Open kirill-develops opened 1 year ago

kirill-develops commented 1 year ago

Bug report

Current Behavior

I'm using the component wrapper and in a nested component the useFormikContext() hook. When calling the resetForm() method from useFormikContext, it doesn't actually reset the touched, errors or values to initialValues set in the wrapper. I have to call setTouched(), setError() and setValues() individually to achieve the result expected.

Expected behavior

When calling resetForm() the state is reset to the initialized values.

Reproducible example

Tried using the above sandbox however useFormikContext() was throwing an error (not a method of formik)

Your environment

Software Version(s)
Formik "^2.2.9"
React "18.2.0",
TypeScript n/a
Browser chrome & brave
npm/Yarn 8.19.2
Operating System macOS Ventura
quantizor commented 1 year ago

Do you have a reproduction sandbox?

donaminos commented 1 year ago

I am experiencing a similar issue in my codebase. it was working fine with React 17 but the upgrade to React 18 broke this behaviour.

Update: I could fix it by using flushSync from react-dom

flushSync(() => {
  resetForm();
});
vennilamahalingam commented 12 months ago

I am not able to reproduce the issue. Refer : https://codesandbox.io/s/formik-v2-useformikcontext-forked-pc6j9g?file=/index.js Or can you make changes in this sandbox with the exact use case as yours ?

KyuubiTila commented 6 months ago

<Formik initialValues={initialValues} onSubmit={(data, params) => { console.log(data); addProduct(data); params.resetForm(); }} validationSchema={validationSchema}