Option to not reset all form state when only some initialValues change
See original GitHub issueI have a Formik component which initials values depend on props
<Formik
initialValues={{
foo: this.props.foo,
bar: this.props.bar
}}
...
I save each field onBlur
, which also changes the corresponding prop of the component to keep in sync with the updated value on server (using graphql with apollo-client). This change of prop triggers reset form https://github.com/jaredpalmer/formik/blob/master/src/formik.tsx#L236-L238 and with it losing all errors & touched info. Is there any way to shallow compare the values of the initialValues and only reset the ones that changed?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Formik reset form, not to the initial state but a new one
This option should work for you if after submitting, the initialValues are updated to the current state.
Read more >useForm - reset - React Hook Form
Reset the entire form state, fields reference, and subscriptions. There are optional arguments and will allow partial form state reset. Props. Reset has...
Read more >React Hook Form - Reset form with default values and clear ...
The solution is to use the reset() function from the React Hook Form library, if you execute the function without any parameters (...
Read more >Final Form Docs – `FormApi`
Resets all form and field state. Same as calling reset(initialValues) on the form and resetFieldState() for each field. Form should be just as...
Read more ><input type="reset"> - HTML: HyperText Markup Language
elements of type reset are rendered as buttons, with a default click event handler that resets all inputs in the form to their...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solution is here: https://github.com/jaredpalmer/formik/pull/181#issuecomment-332594908.
tl;dr: Just use React 😄, componentWillReceiveProps and setValues…
Hey @jaredpalmer . Sorry it took so so long.
Here is the codesandox https://codesandbox.io/s/n31p94m320 .
Here is a demo with the problem, when only one prop changes, the whole forms reset
I think the solution would be to have an option which prevents the form from refreshing dirty values on initialValues change like @VladShcherbin said in his issue #168 . He made some great points, and how redux-form solves those issues.
Another related issue is #172 which mainly has the same problems