Search code examples
javascriptreduxredux-form

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.

Solution

  • 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.