Hero Hook Form API / syncArrays
Function: syncArrays()
syncArrays<
TItem>(options):ArraySyncResult<TItem>
Defined in: utils/arraySync.ts:66
Sync arrays to determine what items to delete, update, and create.
Type Parameters
TItem
TItem
The item type in the arrays
Parameters
options
ArraySyncOptions<TItem>
Sync options
Returns
ArraySyncResult<TItem>
Result with items to delete, update, and create
Description
Compares existing items (from database) with current items (from form) to determine which items need to be deleted, updated, or created. Useful for edit forms where you need to sync array changes.
Example
const { toDelete, toUpdate, toCreate } = syncArrays({
existing: slots,
current: data.slots,
getId: (slot) => slot.id,
});
// Delete removed slots
await Promise.all(toDelete.map(slot => deleteSlot(slot.id)));
// Update existing slots
await Promise.all(
toUpdate.map(({ existing, current }) =>
updateSlot(existing.id, current)
)
);
// Create new slots
await Promise.all(toCreate.map(slot => createSlot(slot)));