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
* Checks for exhaustive useMemo/useCallback dependencies without extraneous values
664
+
*/
665
+
MemoDependencies='MemoDependencies',
661
666
/**
662
667
* Checks for known incompatible libraries
663
668
*/
@@ -1055,6 +1060,16 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
1055
1060
preset: LintRulePreset.RecommendedLatest,
1056
1061
};
1057
1062
}
1063
+
caseErrorCategory.MemoDependencies: {
1064
+
return{
1065
+
category,
1066
+
severity: ErrorSeverity.Error,
1067
+
name: 'memo-dependencies',
1068
+
description:
1069
+
'Validates that useMemo() and useCallback() specify comprehensive dependencies without extraneous values. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
@@ -247,36 +274,39 @@ export function validateExhaustiveDependencies(
247
274
];
248
275
}
249
276
if(missing.length!==0){
250
-
// Error
251
277
constdiagnostic=CompilerDiagnostic.create({
252
-
category: ErrorCategory.PreserveManualMemo,
278
+
category: ErrorCategory.MemoDependencies,
253
279
reason: 'Found non-exhaustive dependencies',
254
280
description:
255
281
'Missing dependencies can cause a value not to update when those inputs change, '+
256
-
'resulting in stale UI. This memoization cannot be safely rewritten by the compiler.',
282
+
'resulting in stale UI',
257
283
suggestions,
258
284
});
259
285
for(constdepofmissing){
286
+
letreactiveStableValueHint='';
287
+
if(isStableType(dep.identifier)){
288
+
reactiveStableValueHint=
289
+
'. Refs, setState functions, and other "stable" values generally do not need to be added as dependencies, but this variable may change over time to point to different values';
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-exhaustive-deps.expect.md
+42-25Lines changed: 42 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,8 +26,16 @@ function Component({x, y, z}) {
26
26
}, [x]);
27
27
constf=useMemo(() => {
28
28
return [];
29
-
}, [x, y.z, z?.y?.a]);
30
-
return<Stringify results={[a, b, c, d, e, f]} />;
29
+
}, [x, y.z, z?.y?.a, UNUSED_GLOBAL]);
30
+
constref1=useRef(null);
31
+
constref2=useRef(null);
32
+
constref= z ? ref1 : ref2;
33
+
constcb=useMemo(() => {
34
+
return () => {
35
+
returnref.current;
36
+
};
37
+
}, []);
38
+
return<Stringify results={[a, b, c, d, e, f, cb]} />;
31
39
}
32
40
33
41
```
@@ -36,11 +44,11 @@ function Component({x, y, z}) {
36
44
## Error
37
45
38
46
```
39
-
Found 4 errors:
47
+
Found 5 errors:
40
48
41
-
Compilation Skipped: Found non-exhaustive dependencies
49
+
Error: Found non-exhaustive dependencies
42
50
43
-
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.This memoization cannot be safely rewritten by the compiler..
51
+
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.
Compilation Skipped: Found non-exhaustive dependencies
62
+
Error: Found non-exhaustive dependencies
55
63
56
-
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.This memoization cannot be safely rewritten by the compiler..
64
+
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.
Compilation Skipped: Found non-exhaustive dependencies
75
+
Error: Found non-exhaustive dependencies
68
76
69
-
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.This memoization cannot be safely rewritten by the compiler..
77
+
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.
Compilation Skipped: Found unnecessary memoization dependencies
81
-
82
-
Unnecessary dependencies can cause a value to update more often than necessary, which can cause effects to run more than expected. This memoization cannot be safely rewritten by the compiler.
Missing dependencies can cause a value not to update when those inputs change, resulting in stale UI.
104
+
105
+
error.invalid-exhaustive-deps.ts:31:13
106
+
29|constcb=useMemo(() => {
107
+
30|return () => {
108
+
>31|returnref.current;
109
+
|^^^ Missing dependency `ref`. Refs, setState functions, and other "stable" values generally do not need to be added as dependencies, but this variable may change over time to point to different values
110
+
32| };
111
+
33| }, []);
112
+
34|return<Stringify results={[a, b, c, d, e, f, cb]} />;
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-exhaustive-deps.js
+10-2Lines changed: 10 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,14 @@ function Component({x, y, z}) {
0 commit comments