Add enableReinitialize option and set it to false by default
See original GitHub issueCurrently, if you pass initialValues
, you can overwrite them by passing another value - the form will update its values.
This brings a problem when your data is stored for example in redux or in another component. You get this data and pass into form. Once this values are changed (in redux / another component) - your form magically updates and removes all data you’ve changed.
In redux-form there is a nice enableReinitialize option which is false
by default. You can set it to true
and your form will update itself.
I think, the same behavior and option should be added to formik so your data won’t suddenly disappear.
Related issues - https://github.com/jaredpalmer/formik/issues/163, https://github.com/jaredpalmer/formik/issues/164
For the first related issue, there is keepDirtyOnReinitialize option which prevents form from refreshing dirty values on initialValues
change.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:6 (5 by maintainers)
Top GitHub Comments
Solution is here: https://github.com/jaredpalmer/formik/pull/181#issuecomment-332594908.
tl;dr: Just use React 😄, componentWillReceiveProps and setValues…
@megazoll
mapPropsToValues
will only pass props that are not functions by default. If you have an edge case, can you share it in a separate issue?@VladShcherbin I have added enableReinitialize to master and initialValues is now passed down to props and through context. Should be trivial to get behavior you desire.