formik icon indicating copy to clipboard operation
formik copied to clipboard

Allow resetting only actually updated initial values

Open DnEgorWeb opened this issue 3 years ago • 16 comments

🚀 Feature request

Allow reinitializing only updated initial values, do not reset the whole form

Current Behavior

Right now enableReinitialize feature reinitialises all the fields to initial values. If we have 2 fields and one of them already has a value, and then another field gets a new initial value, both fields will be reset and 1st field will have initial value again.

Desired Behavior

When components receive new initial value for one of the fields - reset only this field and do not reset all of the other fields.

  1. Initial state
<Formik
  enableReinitialize
  initialValues={{
    first: props.first, // this field is dynamic and depends on props
    second: '',
  }}
  ...
>
  ...
</Formik>
  1. Then components change the second field by itself and now it contains my text (for example). So second has a value of my text now, first is still an empty string.
  2. Then the component receives new props.first and the form is reset. While first gets a new initial value, I want second to preserve its changed value my text, and not to be empty again.

Suggested Solution

Either make enableReinitialize prop to reset only changed fields or add another prop like enableSingleFieldReinitialize which will stand for a flag for enableReinitialize inside useEffect.

Who does this impact? Who is this for?

For all users who need dynamic initial values while modifying the form fields.

Describe alternatives you've considered

enableReinitialize can also accept an object with fields we want to be reinitialised when initial value changes.

DnEgorWeb avatar Sep 17 '20 12:09 DnEgorWeb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] avatar Nov 20 '20 00:11 github-actions[bot]

any updates on this?

ming1in avatar Apr 16 '21 02:04 ming1in

having same issue

innakostiuk avatar May 12 '21 04:05 innakostiuk

Any updates on this? Would be really helpful

Marius-Adam avatar Jun 30 '21 08:06 Marius-Adam

Bumping this. Trying to accomplish the same thing, but can't find a solution!

douglasrcjames avatar Nov 01 '21 20:11 douglasrcjames

Any update? facing same issue.

barjabeen12 avatar Mar 10 '22 13:03 barjabeen12

How is it going?

httol avatar Mar 16 '22 04:03 httol

I also would appreciate a feature like this. Here's a workaround using the useFormik hook:

import { useFormik } from 'formik';

const { setFieldValue } = useFormik({
    initialValues: {
        someField: ''
    },
    onSubmit: () => { console.log('do your submit) },
});

useEffect(() => {
    setFieldValue('someField', someValue);
}, [someValue]);

In my case someValue comes from the global state. This way you can update single fields without reinitialize the whole form.

marcohaferkamp avatar Apr 05 '22 11:04 marcohaferkamp

An effect is the only way to accomplish this in Formik currently

johnrom avatar Apr 05 '22 15:04 johnrom

Still facing this issue. Any solutions?

farrukh28 avatar Aug 18 '22 08:08 farrukh28

At that point (2 years ago) I ended up having my own implementation of form management. Formik does a great job, but it's overloaded with inner logic damaging performance and still not flexible enough in cases like this. I'll keep it open, but it does not look like the feature will be added any time soon.

DnEgorWeb avatar Aug 18 '22 08:08 DnEgorWeb

@DnEgorWeb Agreed. Plus I'm using formik with [email protected] and I think I can't use useFormik hook because formik-mui uses it's own components (Field).

farrukh28 avatar Aug 18 '22 08:08 farrukh28

+1 on this feature request, enableReinitialize needs to be configurable so we can opt in to either resetting the whole form or only parts of the form.

Fals3Android avatar Sep 14 '22 19:09 Fals3Android

+1

nick-0101 avatar Sep 18 '22 12:09 nick-0101

I am facing the same issue. kindly suggest me ,how partially reinitialize the initial value.

Abhishekkumar4444 avatar Oct 26 '22 08:10 Abhishekkumar4444