Skip to content

Commit da424f9

Browse files
committed
Allow useEffect(fn, undefined) in react-hooks/exhaustive-deps. (#27525)
## Summary There is a bug in the `react-hooks/exhaustive-deps` rule that forbids the dependencies argument from being `undefined`. It triggers the error that the dependency list is not an array literal. This makes sense in pre ES5 strict-mode environments as undefined could be redefined, but should not be a concern in today's JS environments. **Justification:** * The deps argument being undefined (for `useEffect` calls etc.) is a valid use case for hooks that should re-run on every render. * The deps argument being omitted is considered a valid use case by the `exhaustive-deps` rule already. * The TypeScript type definitions support passing `undefined` because hooks are typed as `useEffect(effect: EffectCallback, deps?: DependencyList): void;`. * Since omitting an argument and passing `undefined` are considered equivalent, this eslint rule should consider them as equivalent too. Further, I accidentally forgot passing a dependency array to `useEffect` in code that I shared on Twitter, and people started abusing me about it. I'd like to create an eslint rule for my projects that requires me to provide a dep argument in all cases (`undefined`, `[]` or the list of dependencies) so that I can avoid such problems in the future. This would also force me to always think about the dependencies instead of accidentally forgetting them and my hook running on each render. In an audit of my own codebase I had about 3% of hooks that I want to run on each render, and adding an explicit `undefined` seems reasonable in those situations. It could be argued this could be an option or part of the `exhaustive-deps` rule, but it's probably better to merge this PR, make a release and see if my custom eslint rule gains traction in the future. ## How did you test this change? * Added a test. * `yarn test ESLintRuleExhaustiveDeps-test` * Careful code inspection. DiffTrain build for commit d947c2f.
1 parent fcfdb89 commit da424f9

File tree

7 files changed

+9
-9
lines changed

7 files changed

+9
-9
lines changed

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24901,7 +24901,7 @@ function createFiberRoot(
2490124901
return root;
2490224902
}
2490324903

24904-
var ReactVersion = "18.3.0-canary-77c4ac2ce-20231031";
24904+
var ReactVersion = "18.3.0-canary-d947c2f11-20231101";
2490524905

2490624906
// Might add PROFILE later.
2490724907

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-prod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9039,7 +9039,7 @@ var devToolsConfig$jscomp$inline_1033 = {
90399039
throw Error("TestRenderer does not support findFiberByHostInstance()");
90409040
},
90419041
bundleType: 0,
9042-
version: "18.3.0-canary-77c4ac2ce-20231031",
9042+
version: "18.3.0-canary-d947c2f11-20231101",
90439043
rendererPackageName: "react-test-renderer"
90449044
};
90459045
var internals$jscomp$inline_1226 = {
@@ -9070,7 +9070,7 @@ var internals$jscomp$inline_1226 = {
90709070
scheduleRoot: null,
90719071
setRefreshHandler: null,
90729072
getCurrentFiber: null,
9073-
reconcilerVersion: "18.3.0-canary-77c4ac2ce-20231031"
9073+
reconcilerVersion: "18.3.0-canary-d947c2f11-20231101"
90749074
};
90759075
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
90769076
var hook$jscomp$inline_1227 = __REACT_DEVTOOLS_GLOBAL_HOOK__;

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-profiling.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9465,7 +9465,7 @@ var devToolsConfig$jscomp$inline_1075 = {
94659465
throw Error("TestRenderer does not support findFiberByHostInstance()");
94669466
},
94679467
bundleType: 0,
9468-
version: "18.3.0-canary-77c4ac2ce-20231031",
9468+
version: "18.3.0-canary-d947c2f11-20231101",
94699469
rendererPackageName: "react-test-renderer"
94709470
};
94719471
var internals$jscomp$inline_1267 = {
@@ -9496,7 +9496,7 @@ var internals$jscomp$inline_1267 = {
94969496
scheduleRoot: null,
94979497
setRefreshHandler: null,
94989498
getCurrentFiber: null,
9499-
reconcilerVersion: "18.3.0-canary-77c4ac2ce-20231031"
9499+
reconcilerVersion: "18.3.0-canary-d947c2f11-20231101"
95009500
};
95019501
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
95029502
var hook$jscomp$inline_1268 = __REACT_DEVTOOLS_GLOBAL_HOOK__;

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (
2727
}
2828
"use strict";
2929

30-
var ReactVersion = "18.3.0-canary-77c4ac2ce-20231031";
30+
var ReactVersion = "18.3.0-canary-d947c2f11-20231101";
3131

3232
// ATTENTION
3333
// When adding new symbols to this file,

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,4 @@ exports.useSyncExternalStore = function (
580580
exports.useTransition = function () {
581581
return ReactCurrentDispatcher.current.useTransition();
582582
};
583-
exports.version = "18.3.0-canary-77c4ac2ce-20231031";
583+
exports.version = "18.3.0-canary-d947c2f11-20231101";

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-profiling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ exports.useSyncExternalStore = function (
583583
exports.useTransition = function () {
584584
return ReactCurrentDispatcher.current.useTransition();
585585
};
586-
exports.version = "18.3.0-canary-77c4ac2ce-20231031";
586+
exports.version = "18.3.0-canary-d947c2f11-20231101";
587587

588588
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
589589
if (
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
77c4ac2ce88736bbdfe0b29008b5df931c2beb1e
1+
d947c2f1100dfd006fd91c6e9ed84d42ca7ab088

0 commit comments

Comments
 (0)