Skip to content

Commit 78968bb

Browse files
authored
Validate useEffect without deps too (#15183)
1 parent 4b8e164 commit 78968bb

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

+55
Original file line numberDiff line numberDiff line change
@@ -2969,6 +2969,36 @@ const tests = {
29692969
`and use that variable in the cleanup function.`,
29702970
],
29712971
},
2972+
{
2973+
code: `
2974+
function MyComponent() {
2975+
const myRef = useRef();
2976+
useEffect(() => {
2977+
const handleMove = () => {};
2978+
myRef.current.addEventListener('mousemove', handleMove);
2979+
return () => myRef.current.removeEventListener('mousemove', handleMove);
2980+
});
2981+
return <div ref={myRef} />;
2982+
}
2983+
`,
2984+
output: `
2985+
function MyComponent() {
2986+
const myRef = useRef();
2987+
useEffect(() => {
2988+
const handleMove = () => {};
2989+
myRef.current.addEventListener('mousemove', handleMove);
2990+
return () => myRef.current.removeEventListener('mousemove', handleMove);
2991+
});
2992+
return <div ref={myRef} />;
2993+
}
2994+
`,
2995+
errors: [
2996+
`The ref value 'myRef.current' will likely have changed by the time ` +
2997+
`this effect cleanup function runs. If this ref points to a node ` +
2998+
`rendered by React, copy 'myRef.current' to a variable inside the effect, ` +
2999+
`and use that variable in the cleanup function.`,
3000+
],
3001+
},
29723002
{
29733003
code: `
29743004
function useMyThing(myRef) {
@@ -4457,6 +4487,31 @@ const tests = {
44574487
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
44584488
],
44594489
},
4490+
{
4491+
code: `
4492+
function Thing() {
4493+
useEffect(async () => {});
4494+
}
4495+
`,
4496+
output: `
4497+
function Thing() {
4498+
useEffect(async () => {});
4499+
}
4500+
`,
4501+
errors: [
4502+
`Effect callbacks are synchronous to prevent race conditions. ` +
4503+
`Put the async function inside:\n\n` +
4504+
'useEffect(() => {\n' +
4505+
' async function fetchData() {\n' +
4506+
' // You can await here\n' +
4507+
' const response = await MyAPI.getData(someId);\n' +
4508+
' // ...\n' +
4509+
' }\n' +
4510+
' fetchData();\n' +
4511+
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
4512+
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
4513+
],
4514+
},
44604515
{
44614516
code: `
44624517
function Example() {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export default {
8888
// So no need to check for dependency inclusion.
8989
const depsIndex = callbackIndex + 1;
9090
const declaredDependenciesNode = node.parent.arguments[depsIndex];
91-
if (!declaredDependenciesNode) {
91+
if (!declaredDependenciesNode && !isEffect) {
9292
// These are only used for optimization.
9393
if (
9494
reactiveHookName === 'useMemo' ||
@@ -468,6 +468,9 @@ export default {
468468
},
469469
);
470470

471+
if (!declaredDependenciesNode) {
472+
return;
473+
}
471474
const declaredDependencies = [];
472475
const externalDependencies = new Set();
473476
if (declaredDependenciesNode.type !== 'ArrayExpression') {

0 commit comments

Comments
 (0)