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.

Using enableReinitialize & keepDirtyOnReinitialize => INITIALIZE with keepDirty: undefined

See original GitHub issue

I run into problems using enableReinitialize and keepDirtyOnReinitialize, when using initialValues over state mapping. I need initialValues depending on other values, they can change, so static initialValues at ReduxForm property was not the solution.

The goal was that the form reset leads to the initial values set by state mapping and untouched fields are set to the new defaults if state changes.

class UserForm extends Component {
 ...
}

const mapStateToProps = (state, props) => {
  return {    
    initialValues: state.reducer.defaults,    
  }
};

const mapDispatchToProps = (dispatch) => ({
  actions: bindActionCreators({ ...Actions, ...WizardActions }, dispatch),
  dispatch
});

let UserReduxForm = reduxForm({
  form: formId,
  destroyOnUnmount: false,
  enableReinitialize: true, 
  keepDirtyOnReinitialize: true
})(UserForm);

UserReduxForm = connect(
  mapStateToProps,
  mapDispatchToProps
)(UserReduxForm);

export default UserReduxForm;

If enableReinitialize is true, a reopening of the component leads to a redux-form/INITIALIZE action with meta property keepDirty: undefined, causing all dirty / touched fields to be resetted.

After some investigation I found for me a workaround:

class UserForm extends Component {

componentWillReceiveProps(nextProps) {
    const {  defaultValues, formInitialValues } = nextProps;
    const { dispatch  } = this.props;
    
    if (JSON.stringify(defaultValues) !== JSON.stringify(formInitialValues)) {
      dispatch(initialize(formId, defaultValues, true));
    }
  }

 ...
}

const mapStateToProps = (state, props) => {
  return {    
    defaultValues: state.reducer.defaults,
    formInitialValues: (state.form[formId]&&state.form[formId].initial?state.form[formId].initial:{}),
  }
};

const mapDispatchToProps = (dispatch) => ({
  actions: bindActionCreators({ ...Actions, ...WizardActions }, dispatch),
  dispatch
});

let UserReduxForm = reduxForm({
  form: formId,
  destroyOnUnmount: false,
  //enableReinitialize: true, 
  keepDirtyOnReinitialize: true
})(UserForm);

UserReduxForm = connect(
  mapStateToProps,
  mapDispatchToProps
)(UserReduxForm);

export default UserReduxForm;

It seems to me that keepDirtyOnReinitialize is not evaluated when enableReinitialize is enabled? Manually dispatching via dispatch(initialize(formId, defaultValues, true)); with keepDirty value set to true leads to a redux-form/INITIALIZE action with meta.keepDirty: true and all works fine.

Does I anything misunderstand using state mapping to initialValues?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
laurasilvanicommented, Aug 24, 2017

I am experiencing the same issue as ifranke, a reopening of the component causes the dirty values to be resetted to initialValues.

Here is my code:

const mapStateToProps = store => ({
  initialValues: store.evaluations.info.data,
})

const mapDispatchToProps = (dispatch) => ({
  fetchEvaluation: (val) => dispatch(fetchEvaluation(val)),
  initializeForm: (formName) => dispatch(initialize(formName))
})

class InfoFunc extends Component {
   ...

    componentWillMount(){
    if (parseInt(this.props.id, 10) !== 0) { // Load evalution data
      this.props.fetchEvaluation(this.props.id)
      this.props.initializeForm('evaluationInfo', this.props.initialValues)
    } else {
      this.props.initializeForm('evaluationInfo', {})
    }
  }
}

let InfoForm = reduxForm({
  form: 'evaluationInfo',
  destroyOnUnmount: false,
  enableReinitialize: true,
  keepDirtyOnReinitialize: true
})(InfoFunc)

InfoForm = connect(
  state => ({
    initialValues: state.evaluations.info.data
  })
)(InfoForm)

export default (connect(mapStateToProps, mapDispatchToProps)(InfoForm));
4reactions
mghcommented, Nov 22, 2016

@ifranke I ran into this same issue, but when I forked the repo to fix it, the problem went away. The relevant line adding the missing parameter to initialize is here: https://github.com/erikras/redux-form/commit/0ef019cf994da676f7b8997f95868db3166ced55#diff-d749bb1257f721af37259e0346e623beL135

I’m guessing we are just waiting for a new release to fix this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using enableReinitialize & keepDirtyOnReinitialize ... - GitHub
If enableReinitialize is true, a reopening of the component leads to a redux-form/INITIALIZE action with meta property keepDirty: undefined, ...
Read more >
Redux form - enableReinitialize override previous data and ...
In order to solve this issue, I've tried to use "keepDirtyOnReinitialize" but nothing seems to change and I don't really know how to...
Read more >
Initializing from State - Redux Form
To keep dirty form values when it reinitializes, you can set keepDirtyOnReinitialize to true. By default, reinitializing the form replaces all dirty values ......
Read more >
redux-form enablereinitialize, redux-form reinitialize, redux-form ...
To keep dirty form values when it reinitializes, ... In the docs: When [keepDirtyOnReinitialize is] set to true and enableReinitialize is also set,...
Read more >
A higher order component decorator for forms using Redux ...
function redux-form.initialize (form, values, keepDirty) ... _defaultShouldValidate2.default, enableReinitialize: false, keepDirtyOnReinitialize: false, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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