sonnyblack
sonnyblack

Reputation: 1

React Formik form - remember values onChange and reset them on submit

I'm using Formik to create form in my react app. There are few tabs on view, and all of them contain same form (form with same fields). I want some fields to be remembered while user switches tabs. What came to my mind is to save values into Redux on field onChange event but according to Formik documentation it is not recommended to save Formik values into Redux state. Also, on form submit, I want these values to be cleared/reset. What would be the best approcah?

Upvotes: 0

Views: 1936

Answers (1)

Mosè Raguzzini
Mosè Raguzzini

Reputation: 15821

when switching tabs you can pass initialValues to the new form.

  • you can store it in Redux and provide the stored data to initialValues
  • you can store it in upper state and provide the stored data to initialValues

The trick is do not store actual values but create at runtime an object to use as initialValues

Upvotes: 1

Related Questions