Skip to content

Commit 457d123

Browse files
committed
Sidestep compiler alert about useCallback wrapping debounce, due to it not being recognized as returning an inline function reference
React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead react-hooks/exhaustive-deps
1 parent d265500 commit 457d123

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

ui/pages/bridge/prepare/prepare-bridge-page.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,20 @@ const PrepareBridgePage = ({
409409
],
410410
);
411411

412-
const debouncedUpdateQuoteRequestInController = useCallback(
412+
// `useRef` is used here to manually memoize a function reference.
413+
// `useCallback` and React Compiler don't understand that `debounce` returns an inline function reference.
414+
// The function contains reactive dependencies, but they are `dispatch` and an action,
415+
// making it safe not to worry about recreating this function on dependency updates.
416+
const debouncedUpdateQuoteRequestInController = useRef(
413417
debounce((...args: Parameters<typeof updateQuoteRequestParams>) => {
414418
dispatch(updateQuoteRequestParams(...args));
415419
}, 300),
416-
[dispatch],
417420
);
418421

419422
useEffect(() => {
420423
return () => {
421-
debouncedUpdateQuoteRequestInController.cancel();
424+
// This `ref` is safe from unintended mutations, because it points to a function reference, not any reactive node or element.
425+
debouncedUpdateQuoteRequestInController.current.cancel();
422426
};
423427
}, []);
424428

@@ -443,8 +447,10 @@ const PrepareBridgePage = ({
443447
Boolean,
444448
) as string[],
445449
};
446-
debouncedUpdateQuoteRequestInController(quoteParams, eventProperties);
447-
// eslint-disable-next-line react-hooks/exhaustive-deps
450+
debouncedUpdateQuoteRequestInController.current(
451+
quoteParams,
452+
eventProperties,
453+
);
448454
}, [quoteParams]);
449455

450456
// Use smart slippage defaults
@@ -780,7 +786,7 @@ const PrepareBridgePage = ({
780786
if (!quoteParams) {
781787
return;
782788
}
783-
debouncedUpdateQuoteRequestInController(quoteParams, {
789+
debouncedUpdateQuoteRequestInController.current(quoteParams, {
784790
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
785791
// eslint-disable-next-line @typescript-eslint/naming-convention
786792
stx_enabled: smartTransactionsEnabled,

0 commit comments

Comments
 (0)