Skip to content

Commit 329f4d2

Browse files
committed
Allow useEffect(fn, undefined) in react-hooks/exhaustive-deps.
1 parent 309c8ad commit 329f4d2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,15 @@ const tests = {
14521452
}
14531453
`,
14541454
},
1455+
{
1456+
code: normalizeIndent`
1457+
function MyComponent() {
1458+
useEffect(() => {
1459+
console.log('banana banana banana');
1460+
}, undefined);
1461+
}
1462+
`,
1463+
},
14551464
],
14561465
invalid: [
14571466
{

packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,12 @@ export default {
11611161
const callback = node.arguments[callbackIndex];
11621162
const reactiveHook = node.callee;
11631163
const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name;
1164-
const declaredDependenciesNode = node.arguments[callbackIndex + 1];
1164+
const maybeNode = node.arguments[callbackIndex + 1];
1165+
const declaredDependenciesNode =
1166+
maybeNode &&
1167+
!(maybeNode.type === 'Identifier' && maybeNode.name === 'undefined')
1168+
? maybeNode
1169+
: undefined;
11651170
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);
11661171

11671172
// Check whether a callback is supplied. If there is no callback supplied

0 commit comments

Comments
 (0)