-
-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(react-form): removing array items (#1171) #1319
base: main
Are you sure you want to change the base?
Conversation
Can we add tests to verify that we don't regress problems? |
Sure. Sorry, I got so excited that I managed to fix it after a long debugging session that I forgot about the tests. 😸 |
59f92ea
to
537c513
Compare
@crutchcorn I added a test and found another problem that I resolved. After my first change, the array fields started rendering correctly, but the displayed values were wrong. The reason was that The flow:
To fix this I added new code that modifies how setValue is called when a name change is detected. |
537c513
to
9921de9
Compare
View your CI Pipeline Execution ↗ for commit 39e82b3.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1319 +/- ##
==========================================
+ Coverage 88.86% 88.88% +0.01%
==========================================
Files 28 28
Lines 1275 1277 +2
Branches 332 335 +3
==========================================
+ Hits 1133 1135 +2
+ Misses 127 126 -1
- Partials 15 16 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Can you please also add some tests to validate the new default logic? At first the fix seems to be working, well done :) |
The line not covered by tests is an old logic line. I needed to add a new 'if' branch, so I refactored the code slightly to make it easier to understand. The default value logic remains the same and should be covered by the tests introduced here: GitHub Pull Request #445. I only added an exception to not use the default value when the name has changed. This is covered by the array tests, where I check if the values are correct after removing an element. If you want, I can add some tests, but they would duplicate existing ones (setting the default value on mount). Thanks! |
It is impossible to remove an item from an array while properly using React keys.
Here is an example taken from the issue that demonstrates the problem. Try to remove the middle item.
This issue occurs because we use the index inside the field name. When you remove any item other than the last one, the array is re-iterated, and in the next render, the field names change.
In the current code, you update the name as the last action and use the old name in earlier actions.
Let's assume you have an array:
[{ id: 1, name: "one"}, {id: 2, name: "two"}, {id: 3, name: "three"}]
, and you remove the middle item. In the next render loop, you want to render[{ id: 1, name: "one"}, {id: 3, name: "three"}]
. However, for the item with id = 3, the store tries to access the value using the name"items[2].value"
instead of"items[1].value"
, which causes unexpected effects (see the example).This bug doesn't occur when you use the index as a key (which is not correct). In this scenario, React behaves a little differently and doesn't cause a re-render.
I'm not familiar with the entire codebase, so I don't know if this change could have any side effects.