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.

fields.remove (and non-closure pattern) always removes last element in FieldArray

See original GitHub issue

I am using onClick={() => fields.remove(index)} from the docs, and have also tried the non-closure workaround outlined in issue #1128, and the example provided in release 1.2.1.

In each case, no matter which FieldArray element I click ‘close’ on, it always removes the last element in the FieldArray.

If I add console.log(index) to the onClick method, I see that the appropriate index number is logged to the console, but still the last element in FieldArray is what gets removed. 😕 Indeed, I can call fields.remove(0) and still the last element gets popped.

Usually I’d assume I’ve done something wrong - and that remains a strong possibility - but, I have followed the examples closely and it’s quite straightforward.

TL;DR; fields.remove(index) is behaving like fields.pop().

I am using "redux-form": "6.3.2"

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12

github_iconTop GitHub Comments

4reactions
samuelg0rd0ncommented, Jun 16, 2017

@markau Could you please explain what you mean by “the solution is to use componentWillReceiveProps to refresh the props in the component.”? I don’t understand what component you are talking about, what props to refresh and how.

1reaction
markaucommented, Feb 23, 2017

hey @gustavohenke, the root cause of the problem is where the renderField (using terminology from the members/hobbies example) includes a component which uses componentDidMount to set the state.

The complex component (versus the simple <input type="text" {...input} />) from the members example) does not have its props ‘refreshed’ when the fields array changes.

So, definitely not a bug in redux-form, but in implementation of more complex components. I’ll close this again 😆.

For posterity, the solution is to use componentWillReceiveProps to refresh the props in the component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formik arrayhelpers.remove() method always removes the last ...
When you are doing crud with array,Never use index and If you are only rendering static array elements then use index as key....
Read more >
[Video] Using React Hook Form Field Array in a Modal Form
Use React Hook Form fields Array functionality to create and Ionic Framework React Components ... 26:24 Deleting Items from the form array
Read more >
React FieldArray Form Tutorial: Using Formik, Yup ... - YouTube
Introduction to FieldArray + Show the final application we will get at the ... the Formik FieldArray form with React and Yup for...
Read more >
React Formik Tutorial - 22 - FieldArray component - YouTube
Transcript · React Formik Tutorial - 23 - FastField Component · React Formik Tutorial - 18 - Field Revisited · Formik (React Forms)...
Read more >
vertical head differences: Topics by Science.gov
The far field transmission beam pattern in the vertical plane and the waveforms of ... The dominant processes of erosion included removal of...
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