question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to not reset all form state when only some initialValues change

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
jaredpalmercommented, Sep 27, 2017

Solution is here: https://github.com/jaredpalmer/formik/pull/181#issuecomment-332594908.

tl;dr: Just use React 😄, componentWillReceiveProps and setValues…

2reactions
bogdansoarecommented, Sep 24, 2017

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 formik-issue

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found