Utilizing a fieldArray, I am constructing a form with react-hook-form. This form serves the dual purpose of creating and updating a user, and makes use of the "values" prop from react-hook-form. The field within the fieldArray API consists of two basic time inputs, accompanied by an "add" button that generates additional time inputs (initially empty).
The issue arises when fetching data prior to initializing this form, whereby if there is any breakTime present in the array field, it fails to render promptly on the UI. However, upon clicking the add button, all fields retrieved from the API are rendered simultaneously. (refer to images below)
all fields filled, except the array field
after clicking on add button of array field
My code snippet:
const form = useForm<OperatorOrManageData>({
resolver: zodResolver(operatorOrManagerSchema),
defaultValues: initializeDefaultValues(),
mode: "all",
values: data,
})
const { fields, append, remove } = useFieldArray({
name: "breakTime",
control: form.control,
})
{fields.map((field, index) => (
<div className="...">
...
</div>
))}
I attempted implementing a controlled field array as outlined in the documentation https://react-hook-form.com/docs/usefieldarray, but was unsuccessful.