39

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.
davnicwil
  • 28,487
  • 16
  • 107
  • 123
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • 2
    I'm having the same problem. I'm creating a login page. It works fine until when I don't show the login form anymore and my state gets wiped out from the `redux-form/DESTROY`. Did you figure out a solution? – Brennan Cheung May 09 '16 at 06:49
  • 2
    It looks like in my case it was programmer error. Maybe this helps someone else who made the same mistake. My reducer was returning `initialState` for the default case instead of just `state`. Fixed now. – Brennan Cheung May 09 '16 at 06:59

2 Answers2

78

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.

davnicwil
  • 28,487
  • 16
  • 107
  • 123
2

You are probably merging redux-forms's state into yours, you should have it under separate key. Destroy action returns undefined, which is okay, if the redux-forms reducer only manages it's part of the store.

Make sure you're following step #1 in this tutorial, specially the form: formReducer part : https://redux-form.com/7.2.3/docs/gettingstarted.md/#step-1-of-4-form-reducer

Jeff Sallans
  • 110
  • 1
  • 6
george.cz
  • 407
  • 2
  • 6