You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -342,13 +345,37 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
342
345
}
343
346
}
344
347
345
-
console.log('setStateCalls: ',setStateCalls);
346
-
console.log('effectSetStates: ',effectSetStates);
347
348
constthrowableErrors=newCompilerError();
348
349
for(consterroroferrors){
350
+
letreason;
351
+
letdescription='';
352
+
// TODO: Not sure if this is robust enough.
353
+
/*
354
+
* If we use a setState from an invalid useEffect elsewhere then we probably have to
355
+
* hoist state up, else we should calculate in render
356
+
*/
357
+
if(
358
+
setStateCalls.get(error.setStateName)?.length!=
359
+
effectSetStates.get(error.setStateName)?.length
360
+
){
361
+
reason=
362
+
'Consider lifting state up to the parent component to make this a controlled component. (https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes)';
363
+
}else{
364
+
reason=
365
+
'You may not need this effect. Values derived from state should be calculated during render, not in an effect. (https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state)';
366
+
}
367
+
368
+
if(error.propInfo!==undefined){
369
+
description+=error.propInfo;
370
+
}
371
+
372
+
if(error.localStateInfo!==undefined){
373
+
description+=error.localStateInfo;
374
+
}
375
+
349
376
throwableErrors.push({
350
-
reason: `You may not need an effect. Values derived from state should be calculated in render, not in an effect. `,
351
-
description: `You are using a value derived from props${error.propInfo} to update local state in an effect.`,
0 commit comments