diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index b479ce48521..37391a5cf61 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -1485,6 +1485,13 @@ const tests = { } `, }, + { + code: normalizeIndent` + function MyComponent({byId}) { + return useMemo(() => Object.keys(byId), undefined); + } + `, + }, { // Test settings-based additionalHooks - should work with settings code: normalizeIndent` diff --git a/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts b/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts index 05321ffb46f..ce319589d5a 100644 --- a/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts +++ b/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts @@ -1332,11 +1332,10 @@ const rule = { const reactiveHookName = 'name' in nodeWithoutNamespace ? nodeWithoutNamespace.name : ''; const maybeNode = node.arguments[callbackIndex + 1]; + const isExplicitUndefined = + maybeNode?.type === 'Identifier' && maybeNode.name === 'undefined'; const declaredDependenciesNode = - maybeNode && - !(maybeNode.type === 'Identifier' && maybeNode.name === 'undefined') - ? maybeNode - : undefined; + maybeNode && !isExplicitUndefined ? maybeNode : undefined; const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName); // Check whether a callback is supplied. If there is no callback supplied @@ -1374,6 +1373,9 @@ const rule = { declaredDependenciesNode.value === null)) && !isEffect ) { + if (isExplicitUndefined) { + return; + } // These are only used for optimization. if ( reactiveHookName === 'useMemo' ||