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.

FieldArray fields.remove() - popping instead of removing.

See original GitHub issue

Potential bug, potential feature request; unclear of intended implementation/behaviour:

I have written a custom ‘combo-field’ component - comprising a text field and an autocomplete/live search; the container component (rendered using <Field .../> is implemented very similarly to the react-rte example here (but contains 2 inputs instead of 1) while the FieldArray is implemented as in the docs example, including the method of adding new fields dynamically.

I have followed this thread, which mentions the behaviour I am experiencing: when using fields.remove(index), the index appears to be ignored and the last item in the array is popped. Unfortunately the fix is eluding me.

I can add fields successfully; using fields.push({}) inside a button onClick handler fires the ARRAY_PUSH action, adding an empty object to the correct form.formName.values index in the store. A REGISTER_FIELD action is fired, adding a named object to form.formName.registeredFields with name/type/count properties. Updating the values of any input works as intended/expected.

When using fields.remove(), an ARRAY_REMOVE action is fired with the correct index of the array item in the payload. Subsequently, an UNREGISTER_FIELD action is fired, however, the payload for this action contains the name of the last field in the collection. The last field is then removed.

When swapping out the combo-field component for a (either class based or stateless) component containing a single input, this doesn’t happen.

When rendering a simple text <input> either inside the container component’s render() method, or supplied directly to the Field’s component prop, this doesn’t happen.


A repeated combo field seems like a straight-forward use-case, but I could be tackling this the wrong way; perhaps I should be splitting out the input components and not using the container for both as I am? I am open to suggestions, but in case this is actually a bug, there’s the report 😃


Environment

React 15.3.2, Redux 3.6.0, Redux Form 6.5.0


Edit: I’m starting to think this crux of this issue has something to do with my desire to save multiple values from one conceptual ‘input’ (I wrongly presumed redux-form’s input.onChange could handle objects, but the docs indicate strings only 😕)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:30 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
aakashkhatter1296commented, Apr 25, 2018

@ekeyur change your key to key={${index}_${fields.length}}

6reactions
LeshemNoacommented, Dec 29, 2019

I am still facing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FieldArray fields.remove() - popping instead of removing.
Potential bug, potential feature request; unclear of intended implementation/behaviour: I have written a custom 'combo-field' component ...
Read more >
Remove item from redux form field array by its name and not ...
I am trying to delete multiple of items from it but remove() method only works for a single removal perhaps because each time...
Read more >
FieldArray - Redux Form
fields.pop() : Function. Removes an item from the end of the array. Returns the item removed. This is not a mutator; it dispatches...
Read more >
<FieldArray /> | Formik
<FieldArray /> is a component that helps with common array/list manipulations. You pass it a name property with the path to the key...
Read more >
How to delete a value from an array in JavaScript - byte archer
This classic question pops up once in a while. Even the creator of Node.js Ryan ... The correct way to remove an item...
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