Replies: 19 comments 44 replies
-
Not sure why I use |
Beta Was this translation helpful? Give feedback.
-
You can rename the field array const { fields } = useFieldArray({
keyName: "customId"
})
fields.map(field => field.customId) |
Beta Was this translation helpful? Give feedback.
-
Additionlay, from v7.23.0, the
|
Beta Was this translation helpful? Give feedback.
-
I think you misunderstood the docs. The
I am using version 7.34.2 and am getting the |
Beta Was this translation helpful? Give feedback.
-
Please, check out this csb and see if you repro the issue. |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 How do I get useFieldArray to use the I could make a separate, |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 I don't think you understood the issue. When I get the data after the form is submitted with the data from handleSubmit, passed to my onSubit function, the keys are still in it! The data is dirty. That is the issue. Dirty data. |
Beta Was this translation helpful? Give feedback.
-
It comes back to my original post, where I said the keys had to be "stripped out". I was expounding on that issue. |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 @leapful Here is a codesandbox of the issue. First, it loads the data called 'dataFromApi', and loads it into useFieldArray, then, useFieldArray overwrites all the ids. I also made a codesandbox that uses the keyName prop Check the logs on lines 20 and 21, and you'll see the outgoing objects are altered from the incoming ones Object before:
Object after:
So if I were to save the data back to the server, the 'key' prop would be added to all of my objects, hence what I said about dirtying the data. Now, you could just ask the users to clean their own data, but may I offer a solution? I think that a check could be implemented on incoming data with the key of 'id', that checks if it is already in a uuid/guid-type format, and if it is, will use it instead of overwriting it.
I just think it would improve the dev experience. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Detailed answer > #8935 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
rather than the solution above (thank you for explaining), I wonder if it's conceptually easier to keep the Any thoughts anyone? And thanks for this library. |
Beta Was this translation helpful? Give feedback.
-
I also see a usecase for |
Beta Was this translation helpful? Give feedback.
-
Hi all I am a bit worried here. If edit: e.g. something along the lines |
Beta Was this translation helpful? Give feedback.
-
I understand that RHF needs the Assume you do something like: type FormData = {
foo: FieldModel[]
}
const form = useForm<FormData>({
resolver: zodResolver(formDataSchema),
defaultValues
});
const { fields } = useFieldArray({
control: form.control,
name: 'foo'
}); I suggest that the type of One alternative, that would avoid all sorts of clashes and issues, is that Let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
Hi, I also recently found out RHF's use of the id field in arrays and the future deprecation of keyName. I would like for it to either not be deprecated or have an alternative. Here's my use case. I use RHF to collect data in a form with dynamic fields. The form data structure is: type nestedItemsList = {
id: string
name: string
items: {
id: string
name: string
subItems: {
id: string,
name: string
}[]
}[]
} The data is fed in a database which uses exactly the same data structure. Id fields are used internally as unique identifiers for the items and subitems, and are automatically assigned by the database. When I need to update a nestedItemsList and add a new item, its id field is thus significant for the db: if undefined the item will be newly created, if not the corresponding item will be searched and updated. To prevent RHF from meddling with id fields and breaking the db logic, I currently use This is the only issue I have with this otherwise excellent library :) |
Beta Was this translation helpful? Give feedback.
-
https://stackoverflow.com/questions/77289582/react-hook-form-usefieldarray-overwriting-my-own-id |
Beta Was this translation helpful? Give feedback.
-
Just to clarify the previous workarounds (they weren't clear enough to me): const { fields, append, remove } = useFieldArray({
control,
name: 'something',
});
const form = useFormContext();
const realValues = form.getValues('myArrayField');
// ...
{fields.map((row, index) => (
<tr key={row.id}>
<td>
<input
type="text"
defaultValue={realValues[index].id}
/>
</td>
</tr>
));} |
Beta Was this translation helpful? Give feedback.
-
I'm also using the keyName to solve a problem where I send a delete request from inside a modal form, and do an "optimistic update" using remove. it works well with keyName. The reason I do it this way is because the modal form overlays my main database object, and if I refresh the data, the modal will disappear and need to be reopened again. What other ways are peope using to delete an array element and do an optimistic update? |
Beta Was this translation helpful? Give feedback.
-
I almost pulled my hairs off trying to debug for hours why my ids were not matching in the code when moving the existing logic from formik to react-hook-form. This is terrible design. The developer should be responsible for managing the ids, or at least make sure that id field is not overwritten if it already exists. Or don't remove the keyName prop from the hook in the next version. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
If the array is populated by data from a database, the data most likely has an
id
field.useFieldArray
generates its own UUID asid
and it overwrites this property. Imagine if I have a delete button for every item in the list and sends a delete request to the server using theid
, the server would not find thisid
because it's generated and overwritten byuseFieldArray
Describe the solution you'd like
Rename
id
to something like_id
or even something more unique likerhf_id
.Describe alternatives you've considered
All my tables in the database have a
id
as primary key. I could rename all of them, but seriously.Beta Was this translation helpful? Give feedback.
All reactions