Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values via props.change() in componentDidMount() will be overwritten #3706

Closed
beckerei opened this issue Dec 21, 2017 · 8 comments · Fixed by #3797
Closed

Values via props.change() in componentDidMount() will be overwritten #3706

beckerei opened this issue Dec 21, 2017 · 8 comments · Fixed by #3797

Comments

@beckerei
Copy link

beckerei commented Dec 21, 2017

I assume this is a bug, since it worked in previous versions of redux-form.
Edit: Can confirm it worked in 7.1.2

We have a form with a few fields which may have a default value. Those are set via props.change(),
see the shortened code below.

componentWillMount() {
  this.preSelectCountry = locale === 'de';
}

componentDidMount() {
    if (this.preSelectCountry) {
      this.props.change('country_code', Object.keys(this.CountryList)[0]);
    }
}

What is the current behavior?

@@redux-form/INITIALIZE is dispatched as first action, followed a couple of @@redux-form/REGISTER_FIELD
@@redux-form/CHANGE is dispatched with correct values. country_code is a registered field the action results in the correct change of the values state for country_code.
Then again @@redux-form/INITIALIZE is dispatched resulting in overwriting the previously set values. Then fields are registered and unregistered again.

To this time there is not a single action which is not dispatched by redux-form.
Some information to set the initial value of those fields are only available as props, I am unsure about how to usereduxForm({ ...initialValues: { ... }}) to do this. This would work, since we use this method to set a initial value to a field.

Form Validation will somehow consider those set values, they are even displayed correctly. But are missing when submitting.

Note: There is not much more logic in this app besides sending the form to server and displaying a result page.

What is the expected behavior?

I don't expect the form the initialize twice, resulting in overwriting previously set data.

Sandbox Link

I will setup a demo for this as soon as I have time for it.

Edit: I condensed the application we have: https://codesandbox.io/s/konx6r9zpv

What's your environment?

redux-form: 7.2.0
the app is tested with: Chrome, Firefox, Safari, IE10/11 (😢) on osx/window

@inPhoenix
Copy link

I noticed the same issue. Basically, the componentDidMount is not working to change/dispatch(change) values in the form. I had the same logic working on 7.0.3 and it is not working on 7.2.0.

@beckerei
Copy link
Author

beckerei commented Jan 3, 2018

btw. sorry I haven't had the time to create a sandbox for this problem.
A fork of the simple form example with some modifications did not have the issue. I need to dig a little deeper to find the issue. Maybe I try to reduce our repo until I stripped out everything that not caused this.

@liubo13
Copy link

liubo13 commented Jan 4, 2018

I also encountered the same problem. It is working on 7.1.2 and it is not working on 7.2.0.

@beckerei
Copy link
Author

beckerei commented Jan 5, 2018

I managed to condense our app to a much simpler version with only to fields the bug still is there.

https://codesandbox.io/s/konx6r9zpv

The second field keeps its inital value, the first fields value is overwritten by the second INITIALIZE of the form. I included the redux devtools extension.

Happy to help with fixing this 👍

@beckerei
Copy link
Author

beckerei commented Jan 7, 2018

If it does not introduce any side effects in your app you can set keepDirtyOnReinitialize to true, updated the sandbox and added this option as comment.

Prior to 7.2.0 initIfNeeded was only called within componentWillMount() and componentWillReceiveProps(), in 7.2.0 it is called in componentDidMount() as well, the code is duplicated from componentWillMount.

@martinhorsky
Copy link
Contributor

Duplicate of #3690

@erikras
Copy link
Member

erikras commented Feb 1, 2018

Published fix in v7.2.2.

@lock
Copy link

lock bot commented Feb 1, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants