react-reactive-form icon indicating copy to clipboard operation
react-reactive-form copied to clipboard

FormArrary Delete not working at index level removeAt(index) are we have any solutions to delete by index

Open sathishobili opened this issue 5 years ago • 8 comments

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

sathishobili avatar Jun 10 '19 16:06 sathishobili

You can check the working example of removeAt here

bietkul avatar Jun 11 '19 11:06 bietkul

@bietkul actually that example shows exactly the problem I'm having. Try to add to new items to the array, then type "A" as the first item's name, then " B" as the second item's name. Then try to delete the first item, you will se that the B item is removed.

jfrader avatar Jun 13 '19 02:06 jfrader

I've checked this as I'm considering usage of this library and this is something I'd run into very soon. The underlying values are correct, the removeAt method works as it should (the source is ported from angular very precisely, and there it works splendidly). It's the rendering which is problematic here.

See the part where controls are iterated - the key prop for each element is the index. This means that since indexes don't change when mutating the controls array, the fields don't re-render either. Changing the key to something like key={Math.random() * 10000} made it work (ugly, but proves the point).

I'll try to come up with something smarter as soon as I have a similar scenario in my app.

michal-filip avatar Sep 29 '19 19:09 michal-filip

Hi When will this bug be resolved? @bietkul @michal-filip I'm also investigating to fix the problem.

mojtaba-ramezani avatar Oct 11 '19 21:10 mojtaba-ramezani

Hey @mojtaba-ramezani It is an issue with React keys, the keys should be unique while rendering a list. I updated the example, please check it.

bietkul avatar Oct 12 '19 07:10 bietkul

Thankful. The problem is fixed in example @bietkul please close this issue

mojtaba-ramezani avatar Oct 12 '19 10:10 mojtaba-ramezani

The above example will work for single remove. My case i want to remove multiple FormControls from FormGroup then need to push new multiple FormControls in the same FormGroup. At that time removeAt will not worked.

If i loop through my FormGroup & try to use removeAt it will execute only one time then break the loop, also i have other logic codes below that also not executable.

I tried like below code,

control.meta.currentForm.controls['dynamicSelector']['controls'].forEach((child, index) => { child.removeAt(index); });

MartinJesu avatar May 19 '20 15:05 MartinJesu

removeAt is getting issues with using React Hook, also if I tried to generate a from using Json structure, component not getting rerender. Thanks in Advance @bietkul

abhaysoftskill avatar Sep 10 '20 05:09 abhaysoftskill