shadcn-ahooks
useRequest

Debounce

The API useRequest

Debounce

Enter the debounce mode by setting options.debounceWait. At this time, if run or runAsync is triggered frequently, the request will be executed with the debounce strategy.

const { data, run } = useRequest(getUsername, {
  debounceWait: 300,
  manual: true
});

As in the example code above, if run is triggered frequently, it will only wait for 300ms to execute after the last trigger.

You can quickly enter text in the input box below to experience the effect

API

Options

The usage and effect of all debounce property are the same as lodash.debounce

PropertyDescriptionTypeDefault Value
debounceWaitDebounce delay time, in milliseconds. After setting, enter the debounce modenumber-
debounceLeadingExecute the request before the delay startsbooleanfalse
debounceTrailingExecute the request after the delay endsbooleantrue
debounceMaxWaitThe maximum time request is allowed to be delayed before it’s executednumber-
pollingIntervalWhenpollingIntervalWhen is used together with debounce options, it specifies when to start polling(data, params) => number | boolean-

Remark

  • options.debounceWait, options.debounceLeading, options.debounceTrailing, options.debounceMaxWait, options.pollingIntervalWhen support dynamic changes.
  • runAsync will return a Promise when it is actually executed. When it is not executed, there will be no return.
  • cancel can abort a function waiting to be executed.

On this page