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
initialValues overwrites existing form value #370
Comments
This is an interesting edge case. There are two things going on.
However, perhaps Thoughts? |
Yes - that makes sense. We might need to re-consider the name through,
As you've stated, this is an edge case, and only really affects forms using If folks are using From this, my assumption is that most people using Additionally, the name My vote is for |
I'm having the same issue and found the If a form has already had the The |
You make a good point, @webmasterkai. |
👍 I was quite surprised to find initialValues overriding my form. |
+1 for this. Especially @webmasterkai's comments. Spot on. |
subscribing... |
This should be fixed in |
Thank you @erikras 🎉 😄 |
Nice one, thanks @erikras ! |
🙌 gg |
@erikras: As it turns out, I was actually using For example, I have an edit page that is using a form to edit a DB object. I initially fetch the object in a different store, and on form init use initialValues to pass in the data that was fetched. then the user edits the form, I take the values from the form, use it to save a new object, and then update the original store I was referrign to, which re-renders the component that implements the form, passing the new response returned from the server to the Form component. I know this is a weird use case, as the new state should be the same as what the form already has, but it just makes me feel better that the form is getting updated with the values that just came off the server from the save. Is there something that I can use when implementing my form component that will get me back to the previous functionality prior to 4.0.0 that still leaves everyone else happy with the new more sensible implementation of initialValues? |
@erikras I knew someone would use the edge-edge case! |
@duro: Once the server responds, couldn't you just call initialize on the form? |
"No one would ever expect _____ behavior!" is always a false statement. 😄 @duro, what if the library made you call |
I have a grid and a form side-by-side in a component. When the user clicks a row, I was grabbing the rowdata and calling setState to push it into the initialData prop of the form. That loads the data into the form as expected. But now with this change in behavior, if the user clicks a different row, what is the appropriate way to load the new data into the form? Passing it into intialData doesn't work now since the form has already been initialized. |
@erikras definitely +1 for the |
IMHO, initialValues should change only initialData, and initialize action creator and initializeForm props should change not only initialData but also value itself. Isn't it literally 'initialize' mean? However in my app, I would be very happy if the value is overridden when I pass different initialValues as props because of the same reason as @duro. So, as you mentioned,
How about adding overwriteOnInitialize={true} prop with initialValues? |
I think the problem here is that I missed the all-important last big of @webmasterkai's insightful comment.
Okay, how about this:
The
I would think that the rarest case would be where you need to change That sound good? |
@erikras will this require a manual fire of INITIALIZE when the initialValues prop changes in the component? Or will redux-form handle that? |
@erikras that doesn't seem quite right? I have default values (set via |
If someone wants to reinitialize can they just run destroy first? |
That's what I changed my code to do, destroy and then intitialize if I want On Tue, Dec 22, 2015 at 1:56 PM, Kai Curry notifications@github.com wrote:
|
The erikras solution would works for me. I have several forms in different pages/tabs (with react-router) |
Released this fix as |
Hi guys. Just wanted to call to your attention the side effects this decision has caused (linked to this issue above), and possible reopen the debate as to the best behavior. |
@erikras Which issue are you referring to? #588 seems like intended behavior... if you want to reset a form, you should call reset. The fact is that |
@erikras I'm struggling to understand what is the current behaviour of the library. With 6.5.0 I am observing that even if Am I missing something here? |
Hmm, no, this is not happening. Check this example with Redux DevTools open.
That could be because of |
@gustavohenke you are right, the Nevertheless, when I go back to a form which I previously reinitialized with new values by using the I worked around the issue by abstracting away some logic for all my forms: class FiltersScaffolding extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
const { initialize, formValues, filtersDefaults } = this.props;
formValues === undefined && initialize(filtersDefaults);
}
render() {
const {
children,
isFetching,
dirty,
handleSubmit,
submitForm,
initialize
} = this.props;
return (
<form className="stqts-filters"
onSubmit={handleSubmit(values => submitForm(values).then(() => initialize(values)) )}
>
<div className="filters">
{ children }
</div>
<div className="filters-aside">
<SubmitFiltersButton filtersDidChange={dirty} />
<FiltersLoader isLoading={isFetching} />
</div>
</form>
);
}
}
export default connect(
(state, ownProps) => {
const formValues = getFormValues(ownProps.form, state => state.filters)(state);
return {
...ownProps,
formValues
};
}
)(FiltersScaffolding) in |
Is anyone else still having issues here? I have tried everything but my form continues to be re-initialized if My form has an input field into which the user puts an ID for a Salesforce object. Once one is entered and submitted, the program makes an api call and returns some data. I'm hoping to use this data to auto-populate other parts of the form. This works but it destroys all previous user input. If I pre-set the Any help or advice would be hugely appreciated. Thanks! |
@ccnixon I have the exact same issue and it's quite problematic in terms of user experience because my form is automatically submitted after any change (with a debounce), so it feels very annoying to have a text field changing while typing. Setting one of the fields to an empty string as default value in the initialValues={{
name: '',
...query
}} By the way, the issue also persists if It apparently has to do with this block of code: https://github.com/erikras/redux-form/blob/42a9c7e71d859b7f06d32fed97288aa5ebbe6a43/src/createReducer.js#L324-L328 @erikras is there any way to override this behaviour? |
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. |
My form uses
destroyOnUnmount: false
to persist values across route changes but, when a user leaves the page and eventually returns to this form,initialValues
will overwrite the existing user data.Is this by design or a bug?
initialValues
suggests to me the values will only be used initially and ignored after user input.If the latter, I'm happy to code up a test case and solution, otherwise I'll guess I'll have to find a work-around.
The text was updated successfully, but these errors were encountered: