`withFieldGroup` array handling with nested
Describe the bug
When combining the withFieldGroup for a complex array handling, the nested array element seems out of sync with the actual value of the group.
I've just tested while passing the root form to the SingleFieldWithReorder and the nested elements keep being out of sync (regarding the json value displayed)
Also, this is unrelated but when looking at the extra props of the render function, it seems to pass the fields and the form. Idk if this is expected or if the props must be sanitized when rendering to remove theses values before passing the props to the render method. (They are undocumented and not typed)
Your minimal, reproducible example
https://codesandbox.io/p/devbox/misty-monad-8t29lx?workspaceId=ws_PkVpfRWHUp5vMscgD59gVj
Steps to reproduce
- Try to move the value with the
move down|move upbuttons. - See that the values from the base array mapping are correct, but the values rendered in the element aren't.
Expected behavior
I think the nested group value should remain sync with the array mapping from the parent group
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- Browser: Tested on chrome 138.0.7204.183 (Official Build) (64-bit)
TanStack Form adapter
react-form
TanStack Form version
v1.15.1
TypeScript version
No response
Additional context
No response
I encountered the same issue. Looks like this happens even without nesting withFieldGroup components.
https://codesandbox.io/p/devbox/suspicious-elgamal-79j4tr?workspaceId=ws_MqxYqe2Tbvb7TuAH8jpm52
Seems like the reason for this is that useFieldGroup doesn't react to changes to opts.fields.
I suppose FieldGroupApi should have something similar to FieldApi's update method, which will then be used in useFieldGroup:
useIsomorphicLayoutEffect(() => {
formLensApi.update(opts)
})
@LeCarbonator can you confirm?
I suppose that reactive fields should be possible and is not considered by the field group at the moment.
I was initially hesitant to check this since there's array-related issues that should be solved first before determining that this is field group related instead of a core issue of arrays.
The examples help though! I'll have a look sometime.
In case someone needs a quick solution:
Adding a key prop to a withFieldsGroup component when rendering mitigates the issue by forcing it to remount.
Here's an updated sandbox with side by side comparison. https://codesandbox.io/p/devbox/condescending-wind-2p6qk4?workspaceId=ws_MqxYqe2Tbvb7TuAH8jpm52
I also tried using useMemo instead of constant-like useState in useFieldGroup and it solved the issue in my case.
I am willing to investigate it further and hopefully open a PR when I have free time.