From b9338e57071985615ae89f9272ef2680277c87d9 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 5 Apr 2024 10:21:00 +0200 Subject: [PATCH] Interactivity: Return useMemo and useCallback hooks (#60474) Return a value from the useMemo and useCallback hooks. --------- Co-authored-by: sirreal Co-authored-by: gziolo --- packages/interactivity/CHANGELOG.md | 4 ++++ packages/interactivity/src/utils.ts | 30 +++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/interactivity/CHANGELOG.md b/packages/interactivity/CHANGELOG.md index 7f4dd9296facc5..e0b32cee128654 100644 --- a/packages/interactivity/CHANGELOG.md +++ b/packages/interactivity/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fixes + +- Hooks useMemo and useCallback should return a value. ([#60474](https://github.com/WordPress/gutenberg/pull/60474)) + ## 5.4.0 (2024-04-03) ## 5.3.0 (2024-03-21) diff --git a/packages/interactivity/src/utils.ts b/packages/interactivity/src/utils.ts index 595a21362f8593..667473afbe0f6c 100644 --- a/packages/interactivity/src/utils.ts +++ b/packages/interactivity/src/utils.ts @@ -211,13 +211,16 @@ export function useLayoutEffect( callback: Function, inputs: any[] ) { * scope available so functions like `getElement()` and `getContext()` can be * used inside the passed callback. * - * @param callback Imperative function that can return a cleanup - * function. - * @param inputs If present, effect will only activate if the - * values in the list change (using `===`). + * @template {Function} T The callback function type. + * + * @param {T} callback Callback function. + * @param {ReadonlyArray} inputs If present, the callback will only be updated if the + * values in the list change (using `===`). + * + * @return {T} The callback function. */ -export function useCallback( callback: Function, inputs: any[] ) { - _useCallback( withScope( callback ), inputs ); +export function useCallback( callback, inputs ) { + return _useCallback( withScope( callback ), inputs ); } /** @@ -228,13 +231,16 @@ export function useCallback( callback: Function, inputs: any[] ) { * available so functions like `getElement()` and `getContext()` can be used * inside the passed factory function. * - * @param factory Imperative function that can return a cleanup - * function. - * @param inputs If present, effect will only activate if the - * values in the list change (using `===`). + * @template {unknown} T The memoized value. + * + * @param {() => T} factory Factory function that returns that value for memoization. + * @param {ReadonlyArray} inputs If present, the factory will only be run to recompute if + * the values in the list change (using `===`). + * + * @return {T} The memoized value. */ -export function useMemo( factory: Function, inputs: any[] ) { - _useMemo( withScope( factory ), inputs ); +export function useMemo( factory, inputs ) { + return _useMemo( withScope( factory ), inputs ); } /**