Skip to content

[BUG] useForm autoSave debounce leads to unbatched updates #7082

@kruschid

Description

@kruschid

Describe the bug

Hi Refine team, thanks for the great library! I’ve noticed an issue with useForm. When using useForm from @refinedev/core with auto-save enabled, requests are triggered multiple times within the specified debounce window. This means the form state changes are not correctly grouped and debounced, instead, they are sent as individual delayed requests.

Looking at the source of the useForm hook, it appears that multiple instances of asyncDebounce are created. Each instance corresponds to a single form state update rather than grouping changes within the debounce window.

const onFinishAutoSave = asyncDebounce(
(values: TVariables) => onFinish(values, { isAutosave: true }),
props.autoSave?.debounce || 1000,
"Cancelled by debounce",
);

I'm using headless refine with the latest version of Mantine. As a workaround, I wrapped the onFinishAutoSave function returned by useFrom in a useDebouncedCallback hook.

Steps To Reproduce

import {  useForm } from "@refinedev/core";

useForm({
  autoSave: {
    enabled: true,
    debounce: 2000,
  },
  ...
});

Bind form-state changes to onFinishAutoSave(values) and modify any input field. Then observe how in the browser’s Network tab, multiple requests are triggered, even though the changes occur within the same time window of 2000ms.

Expected behavior

Only one request should be sent after the user stops editing or when the debounce interval elapses. No multiple requests for changes occurring within the same window.

Packages

@refinedev/core

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions