FieldArray fields.remove() - popping instead of removing.
See original GitHub issuePotential 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:
- Created 7 years ago
- Comments:30 (1 by maintainers)
Top GitHub Comments
@ekeyur change your key to key={${index}_${fields.length}}
I am still facing this issue.