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
@@ -343,13 +346,37 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
343
346
}
344
347
}
345
348
346
-
console.log('setStateCalls: ',setStateCalls);
347
-
console.log('effectSetStates: ',effectSetStates);
348
349
constthrowableErrors=newCompilerError();
349
350
for(consterroroferrors){
351
+
letreason;
352
+
letdescription='';
353
+
// TODO: Not sure if this is robust enough.
354
+
/*
355
+
* If we use a setState from an invalid useEffect elsewhere then we probably have to
356
+
* hoist state up, else we should calculate in render
357
+
*/
358
+
if(
359
+
setStateCalls.get(error.setStateName)?.length!=
360
+
effectSetStates.get(error.setStateName)?.length
361
+
){
362
+
reason=
363
+
'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)';
364
+
}else{
365
+
reason=
366
+
'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)';
367
+
}
368
+
369
+
if(error.propInfo!==undefined){
370
+
description+=error.propInfo;
371
+
}
372
+
373
+
if(error.localStateInfo!==undefined){
374
+
description+=error.localStateInfo;
375
+
}
376
+
350
377
throwableErrors.push({
351
-
reason: `You may not need an effect. Values derived from state should be calculated in render, not in an effect. `,
352
-
description: `You are using a value derived from props${error.propInfo} to update local state in an effect.`,
0 commit comments