Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux-form is Destroying my state once the component is unmounted, what gives?

I am not passing in any special config settings nor am I setting/or calling Destroy... but my state is being cleaned... anyway to prevent this? I need the state to stick around as I need that data thruout my application.

prev state: I see it in there... via redux-logger action: redux-form/Destroy next state: it's gone. 
like image 954
james emanon Avatar asked Feb 17 '16 08:02

james emanon


People also ask

How do I delete a redux form?

B) Simply unmount your form component For many use cases, you will want to either hide your form component after submission succeeds or navigate away to another page, which will cause redux-form 's default behavior of destroying the form data in the reducer in componentWillUnmount .

How does Redux handle form?

Redux Form is very easy to use and implement, we just have to wrap our form component with the HOC provided by Redux Form and we are good to go. Applying validation to the form is very easy in Redux Form, we can apply validation to all the fields as well as validations for individual fields.

Should I store form data in Redux?

There is no “right” answer for this. Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times.


1 Answers

The form's state subtree is destroyed when the form is unmounted, by design. This is the default and expected behaviour.

From v6.2.1 onwards there is a form config property destroyOnUnmount, which explicitly enables/disables the state-clearing behaviour on a specific form (docs here)

import { reduxForm } from 'redux-form';  reduxForm({   form: 'example',   destroyOnUnmount: false })(...) 

This is useful when you have a form whose state you wish to preserve if the user abandons it halfway though, navigates away, and then returns later.

like image 78
davnicwil Avatar answered Sep 30 '22 18:09

davnicwil