Skip to content

Commit a9061e9

Browse files
author
YASHRDX0001
committed
Fix: set-state-in-use-effect false negative when there are multiple instances
Fixes issue #35291 where the ESLint rule was not catching setState calls in effect callbacks when a component had multiple useState and useEffect hooks. The issue was an overly strict optimization check that skipped analyzing some function expressions. Now all function expressions are properly analyzed for setState calls. Changes: - Removed the faulty optimization check in validateNoSetStateInEffects - Added test fixture to prevent regressions
1 parent 378973b commit a9061e9

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInEffects.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,13 @@ export function validateNoSetStateInEffects(
7474
break;
7575
}
7676
case 'FunctionExpression': {
77-
if (
78-
// faster-path to check if the function expression references a setState
79-
[...eachInstructionValueOperand(instr.value)].some(
80-
operand =>
81-
isSetStateType(operand.identifier) ||
82-
setStateFunctions.has(operand.identifier.id),
83-
)
84-
) {
85-
const callee = getSetStateCall(
86-
instr.value.loweredFunc.func,
87-
setStateFunctions,
88-
env,
89-
);
90-
if (callee !== null) {
91-
setStateFunctions.set(instr.lvalue.identifier.id, callee);
92-
}
77+
const callee = getSetStateCall(
78+
instr.value.loweredFunc.func,
79+
setStateFunctions,
80+
env,
81+
);
82+
if (callee !== null) {
83+
setStateFunctions.set(instr.lvalue.identifier.id, callee);
9384
}
9485
break;
9586
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @loggerTestOnly @validateNoSetStateInEffects
2+
import {useEffect, useState} from 'react';
3+
4+
function Component() {
5+
const [loading, setLoading] = useState(false);
6+
useEffect(() => {
7+
setLoading(true);
8+
}, []);
9+
const [firstName, setFirstName] = useState('');
10+
const [lastName, setLastName] = useState('');
11+
const [fullName, setFullName] = useState('');
12+
useEffect(() => {
13+
setFullName(`${firstName} ${lastName}`);
14+
}, [firstName, lastName]);
15+
return null;
16+
}

0 commit comments

Comments
 (0)