Adding an item to ArrayInput on Edit after removing one brings back the same item that was removed
See original GitHub issueWhat you were expecting: Removing an item from ArrayInput should really remove it.
What happened instead: After removing an item and adding a new one, the previosuly removed item is back.
Steps to reproduce: Start here: codesandbox.io/s/happy-germain-877ypu
- Create a new post using the floating + button in bottom right, fill in the required fields and make sure it has a backlink added. Click on Save and edit.
- When Edit view has opened, click on “Miscellaneous” tab to edit backlinks.
- Remove the backlink.
- Add a backlink.
- Observe, the backlink you previosly removed is now back. I have added a video for clarification:
Other information: There is a workaround which fixed the problem for me:
const AddItemButton = ({ onClick, ...rest }: ButtonProps) => {
const { change, getFieldState } = useForm();
return (
<Button
label="ra.action.add"
{...rest}
onClick={(e) => {
const fieldState = getFieldState("formArrayFieldSource");
const nextIndex = fieldState?.value && Array.isArray(fieldState.value) ? fieldState.value.length : 0;
onClick && onClick(e);
change(`formArrayFieldSource[${nextIndex}]`, {
...getEmptyArrayItem()
});
}}>
<AddCircleOutlineRoundedIcon />
</Button>
);
};
where getEmptyArrayItem
is a function which returns an empty array item, such that it doesn’t have any undefined members.
The idea behind it is to manually make sure the newly added array item is really new.
Environment
- React-admin version: 3.19.10 (tried with 3.19.3 too but locally, same problem)
- Last version that did not exhibit the issue (if applicable): N/A
- React version: 16 locally, but the sandbox uses 17
- Browser: Chrome latest
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:10 (6 by maintainers)
Top Results From Across the Web
The ArrayInput Component - React-admin - Marmelab
To edit arrays of data embedded inside a record, <ArrayInput> creates a list of ... A form iterator is a component accepting a...
Read more >How to edit items inside en Array Input in React admin?
I have an object that contains objects and each of them has an array of items, I want to attach to each of...
Read more >Updating Arrays in State - React Docs
How to add, remove, or change items in an array in React state; How to update an object inside of ... Here is...
Read more >Tutorial Chapter 8: Array Input - 4Js
The program in this chapter allows the user to view and change a list of records ... As each record in the program...
Read more >12: 9.15. JSON Functions and Operators - PostgreSQL
The field/element/path extraction operators return the same type as their ... any non-array input is converted into a single-element array, and then the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I tried running the project locally to be sure it’s not a problem caused by codesandbox. I reproduced the issue both with the latest v3 (react-admin@3.19.11 and @material-ui/core@4.12.3) and also with v4 (react-admin@4.0.0).
So this happens because we don’t provide the values for the new item as explained in react-hook-form documentation.
I can only think of two solutions for this, probably complementary:
newItemData
prop to theArrayInput
that will be passed to theappend
functiondefaultValue
prop. I don’t like this as we try to avoid inspecting children since v4 as it has proven to often be problematic.Open to suggestions.