redux-form: Changing the values of a FieldArray using change action creator after a field has received focus throws exception

When you attempt to replace an entire array of data (using the change action creator) used by a FieldArray component after an element from the existing dataset has been focused, an exception is thrown with the message Cannot delete non-numerical index from an array

I’ve made some changes to the FieldArray example project that can be used to replicate the bug - the changes are in commit https://github.com/sargant/redux-form/commit/497f01e1878dee33932b26f4fb8d0bac60041fc4 on my fork.

In order to replicate the bug:

  • Click “Value set A” to populate the form with a predefined set of data
  • Focus and blur any field in the FieldArray part of the form, e.g. a first name
  • Click “Value set B” to populate the form with a different predefined set of data

The error is thrown instead of the data set changing. If you don’t focus a field before changing the data set, it works fine.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 22
  • Comments: 19 (2 by maintainers)

Most upvoted comments

+1 Facing same issue… @erikras Any plans to fix this bug in upcoming releases? change action is really helpful in big applications. it would be great if we can address this issue. i am using v6.4.0

arrayRemoveAll followed by arrayPush workaround works though…!

Because this issue saw almost no activity for a few months, I’m closing it.

Any news about the fixing of this bug?

It’s solved in 7.3.0, I can confirm also. Pity to see nobody from the dev team showed up 😕

Thank you, I made it work using the almost the same solution.

Instead of removing one by one the missing elements of the array, I removed them all in once thanks to : this.props.RxF.removeAll('members');. That way, the form is re rendered just once when removing. Then, I pushed the new values with newItems.map((newItem) => rxf.array.push('members', newItem));

PS : by the way, you have a typo in your last line -> this.props.RxF.arrayRemove(formName, members[${i}], i); instead of this.props.RxF.arrayRemove(formName, members, i);

FWIW, I’ve just come back to this 18 months later and tested the same diff against 7.3.0 and it seems to work now, so looks like something was fixed at some point, unless someone has another breaking example.

I can confirm this bug:

Throws an Cannot delete non-numerical index from an array error, when one of the fields has been focused before:

change('prices', defaultValues.prices);

Works:

array.removeAll('prices');
defaultValues.prices.forEach(price => array.push('prices', price));