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.

Redux-form does not update initialValues prop of wrapped form

See original GitHub issue

redux-form does not update initialValues prop of wrapped form when receiving new initialProps - even when enableReinitialize is true.

The current shouldComponentUpdate behavior (line 247 in reduxForm.js, of version 6.5.0) consults the array propsToNotUpdateFor (which contains the string ‘initialValues’) and returns false, with the result that the wrapped component never receives the new initialValues prop altogether.

This results in the initialValues prop of the wrapped component to become stale and different than the initialValues prop of the reduxForm component, as well as from the initial values stored in the redux store. This looks to me undesirable, and in contradiction with the documentation (and I believe also the intended semantics) of this prop.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:21
  • Comments:6

github_iconTop GitHub Comments

1reaction
marcusmarchesonicommented, Aug 30, 2017

Hi @d-leeg,

Not sure if the issue is the same, but I struggled some time to render the form with initialValues, went through a few issues here and now I managed to do it.

Apart from “enableReinitialize” and “initialValues”, I had to remove a “null check” from the start of my render method. I was avoiding to render the component until “initialValues” was populated, but I was not receiving new props and it was not being rendered for the second time. After I removed this condition, left the component and form to render “empty” for first time and the values get automatic populated after that.

Hope it helps!

reduxForm({ form: ‘myForm’, enableReinitialize: true})

function mapStateToProps(state) { return { initialValues: state.something }; }

0reactions
DesignByOnyxcommented, Jan 5, 2021

I found a solution which should fix this. Looking at the source code - you can see that they check for any configured immutableProps for strict equality, and this code hasn’t changed in 3 years.

This means you can do the following to achieve the desired result:

reduxForm({
  ...
  enableReinitialize: true,
  immutableProps: ['initialValues']
})(YourForm)

NOTE: this requires that you use immutable initialValues:

const MyComponent = () => {
   // DO NOT DO THIS
   return <MyForm initialValues={{ foo: 'bar' }} />
}

Instead, you can do something like this:

const MyComponent = () => {
   // DO THIS
   const initialValues = useMemo(() => ({ foo: 'bar' }), [])
   return <MyForm initialValues={initialValues} />
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

reduxForm - initialValues did not apply after update
I can observe that after an update, my form component rerenders 2 times. First time its prop "initialized" is set to "true". But...
Read more >
Redux Form - initialValues not updating when using compose ...
How to set focus on a TextField\input using a Formik form when initialValues are passed through\bound to props.object? React is not updating when...
Read more >
Initializing From State - Redux Form
Values provided to the initialValues prop or reduxForm() config parameter will be loaded into the form state and treated thereafter as "pristine".
Read more >
How To Add Initial Values into Your Redux Form - Medium
Because we're using the react-redux library, our connect function has to wrap around the entire reduxForm function in order to pass down all...
Read more >
React JS Redux Form with Initial value, Validations and Props ...
With the help of React Redux Form you need not worry about single component state and it ... There is one text input...
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