Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,19 @@ be returned and any scheduled calls cancelled if any of the arguments change,
including the function to debounce, so please wrap functions created on
render in components in `useCallback`.

_Related_

- <https://docs-lodash.com/v4/debounce/>

_Parameters_

- _args_ `...any`: Arguments passed to Lodash's `debounce`.
- _fn_ `TFunc`: The function to debounce.
- _wait_ `[number]`: The number of milliseconds to delay.
- _options_ `[import('lodash').DebounceSettings]`: The options object.

_Returns_

- `Function`: Debounced function.
- `TFunc & import('lodash').Cancelable`: Debounced function.

<a name="useFocusOnMount" href="#useFocusOnMount">#</a> **useFocusOnMount**

Expand Down
19 changes: 15 additions & 4 deletions packages/compose/src/hooks/use-debounce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ import { useMemoOne } from 'use-memo-one';
*/
import { useEffect } from '@wordpress/element';

/* eslint-disable jsdoc/valid-types */
/**
* Debounces a function with Lodash's `debounce`. A new debounced function will
* be returned and any scheduled calls cancelled if any of the arguments change,
* including the function to debounce, so please wrap functions created on
* render in components in `useCallback`.
*
* @param {...any} args Arguments passed to Lodash's `debounce`.
* @see https://docs-lodash.com/v4/debounce/
*
* @return {Function} Debounced function.
* @template {(...args: any[]) => void} TFunc
*
* @param {TFunc} fn The function to debounce.
* @param {number} [wait] The number of milliseconds to delay.
* @param {import('lodash').DebounceSettings} [options] The options object.
* @return {TFunc & import('lodash').Cancelable} Debounced function.
*/
export default function useDebounce( ...args ) {
const debounced = useMemoOne( () => debounce( ...args ), args );
export default function useDebounce( fn, wait, options ) {
/* eslint-enable jsdoc/valid-types */
const debounced = useMemoOne( () => debounce( fn, wait, options ), [
fn,
wait,
options,
] );
useEffect( () => () => debounced.cancel(), [ debounced ] );
return debounced;
}
1 change: 1 addition & 0 deletions packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"src/higher-order/with-instance-id/**/*",
"src/hooks/use-async-list/**/*",
"src/hooks/use-constrained-tabbing/**/*",
"src/hooks/use-debounce/**/*",
"src/hooks/use-instance-id/**/*",
"src/utils/**/*"
]
Expand Down