@@ -1236,7 +1236,7 @@ const tests = {
1236
1236
errors : [
1237
1237
"React Hook useCallback has a missing dependency: 'local2'. " +
1238
1238
'Either include it or remove the dependency array. ' +
1239
- "Values like 'local1' aren't valid dependencies " +
1239
+ "Outer scope values like 'local1' aren't valid dependencies " +
1240
1240
"because their mutation doesn't re-render the component." ,
1241
1241
] ,
1242
1242
} ,
@@ -1302,7 +1302,7 @@ const tests = {
1302
1302
errors : [
1303
1303
"React Hook useCallback has an unnecessary dependency: 'window'. " +
1304
1304
'Either exclude it or remove the dependency array. ' +
1305
- "Values like 'window' aren't valid dependencies " +
1305
+ "Outer scope values like 'window' aren't valid dependencies " +
1306
1306
"because their mutation doesn't re-render the component." ,
1307
1307
] ,
1308
1308
} ,
@@ -2304,9 +2304,8 @@ const tests = {
2304
2304
errors : [
2305
2305
"React Hook useEffect has a missing dependency: 'state'. " +
2306
2306
'Either include it or remove the dependency array. ' +
2307
- `If 'state' is only necessary for calculating the next state, ` +
2308
- `consider refactoring to the setState(state => ...) form which ` +
2309
- `doesn't need to depend on the state from outside.` ,
2307
+ `You can also write 'setState(state => ...)' ` +
2308
+ `if you only use 'state' for the 'setState' call.` ,
2310
2309
] ,
2311
2310
} ,
2312
2311
{
@@ -2336,9 +2335,8 @@ const tests = {
2336
2335
errors : [
2337
2336
"React Hook useEffect has a missing dependency: 'state'. " +
2338
2337
'Either include it or remove the dependency array. ' +
2339
- `If 'state' is only necessary for calculating the next state, ` +
2340
- `consider refactoring to the setState(state => ...) form which ` +
2341
- `doesn't need to depend on the state from outside.` ,
2338
+ `You can also write 'setState(state => ...)' ` +
2339
+ `if you only use 'state' for the 'setState' call.` ,
2342
2340
] ,
2343
2341
} ,
2344
2342
{
@@ -3066,7 +3064,7 @@ const tests = {
3066
3064
errors : [
3067
3065
"React Hook useEffect has an unnecessary dependency: 'window'. " +
3068
3066
'Either exclude it or remove the dependency array. ' +
3069
- "Values like 'window' aren't valid dependencies " +
3067
+ "Outer scope values like 'window' aren't valid dependencies " +
3070
3068
"because their mutation doesn't re-render the component." ,
3071
3069
] ,
3072
3070
} ,
@@ -3092,7 +3090,7 @@ const tests = {
3092
3090
errors : [
3093
3091
"React Hook useEffect has an unnecessary dependency: 'MutableStore.hello'. " +
3094
3092
'Either exclude it or remove the dependency array. ' +
3095
- "Values like 'MutableStore.hello' aren't valid dependencies " +
3093
+ "Outer scope values like 'MutableStore.hello' aren't valid dependencies " +
3096
3094
"because their mutation doesn't re-render the component." ,
3097
3095
] ,
3098
3096
} ,
@@ -3129,7 +3127,7 @@ const tests = {
3129
3127
'React Hook useEffect has unnecessary dependencies: ' +
3130
3128
"'MutableStore.hello.world', 'global.stuff', and 'z'. " +
3131
3129
'Either exclude them or remove the dependency array. ' +
3132
- "Values like 'MutableStore.hello.world' aren't valid dependencies " +
3130
+ "Outer scope values like 'MutableStore.hello.world' aren't valid dependencies " +
3133
3131
"because their mutation doesn't re-render the component." ,
3134
3132
] ,
3135
3133
} ,
@@ -3168,7 +3166,7 @@ const tests = {
3168
3166
'React Hook useEffect has unnecessary dependencies: ' +
3169
3167
"'MutableStore.hello.world', 'global.stuff', and 'z'. " +
3170
3168
'Either exclude them or remove the dependency array. ' +
3171
- "Values like 'MutableStore.hello.world' aren't valid dependencies " +
3169
+ "Outer scope values like 'MutableStore.hello.world' aren't valid dependencies " +
3172
3170
"because their mutation doesn't re-render the component." ,
3173
3171
] ,
3174
3172
} ,
@@ -3205,7 +3203,7 @@ const tests = {
3205
3203
'React Hook useCallback has unnecessary dependencies: ' +
3206
3204
"'MutableStore.hello.world', 'global.stuff', 'props.foo', 'x', 'y', and 'z'. " +
3207
3205
'Either exclude them or remove the dependency array. ' +
3208
- "Values like 'MutableStore.hello.world' aren't valid dependencies " +
3206
+ "Outer scope values like 'MutableStore.hello.world' aren't valid dependencies " +
3209
3207
"because their mutation doesn't re-render the component." ,
3210
3208
] ,
3211
3209
} ,
@@ -3468,8 +3466,7 @@ const tests = {
3468
3466
errors : [
3469
3467
`The 'handleNext' function makes the dependencies of ` +
3470
3468
`useEffect Hook (at line 11) change on every render. ` +
3471
- `To fix this, move the 'handleNext' function ` +
3472
- `inside the useEffect callback (at line 9). Alternatively, ` +
3469
+ `Move it inside the useEffect callback. Alternatively, ` +
3473
3470
`wrap the 'handleNext' definition into its own useCallback() Hook.` ,
3474
3471
] ,
3475
3472
} ,
@@ -3507,8 +3504,7 @@ const tests = {
3507
3504
errors : [
3508
3505
`The 'handleNext' function makes the dependencies of ` +
3509
3506
`useEffect Hook (at line 11) change on every render. ` +
3510
- `To fix this, move the 'handleNext' function ` +
3511
- `inside the useEffect callback (at line 9). Alternatively, ` +
3507
+ `Move it inside the useEffect callback. Alternatively, ` +
3512
3508
`wrap the 'handleNext' definition into its own useCallback() Hook.` ,
3513
3509
] ,
3514
3510
} ,
@@ -3605,17 +3601,14 @@ const tests = {
3605
3601
` ,
3606
3602
errors : [
3607
3603
"The 'handleNext1' function makes the dependencies of useEffect Hook " +
3608
- "(at line 14) change on every render. To fix this, move the 'handleNext1' " +
3609
- 'function inside the useEffect callback (at line 12). Alternatively, wrap the ' +
3610
- "'handleNext1' definition into its own useCallback() Hook." ,
3604
+ '(at line 14) change on every render. Move it inside the useEffect callback. ' +
3605
+ "Alternatively, wrap the 'handleNext1' definition into its own useCallback() Hook." ,
3611
3606
"The 'handleNext2' function makes the dependencies of useLayoutEffect Hook " +
3612
- "(at line 17) change on every render. To fix this, move the 'handleNext2' " +
3613
- 'function inside the useLayoutEffect callback (at line 15). Alternatively, wrap the ' +
3614
- "'handleNext2' definition into its own useCallback() Hook." ,
3607
+ '(at line 17) change on every render. Move it inside the useLayoutEffect callback. ' +
3608
+ "Alternatively, wrap the 'handleNext2' definition into its own useCallback() Hook." ,
3615
3609
"The 'handleNext3' function makes the dependencies of useMemo Hook " +
3616
- "(at line 20) change on every render. To fix this, move the 'handleNext3' " +
3617
- 'function inside the useMemo callback (at line 18). Alternatively, wrap the ' +
3618
- "'handleNext3' definition into its own useCallback() Hook." ,
3610
+ '(at line 20) change on every render. Move it inside the useMemo callback. ' +
3611
+ "Alternatively, wrap the 'handleNext3' definition into its own useCallback() Hook." ,
3619
3612
] ,
3620
3613
} ,
3621
3614
{
@@ -3673,17 +3666,14 @@ const tests = {
3673
3666
` ,
3674
3667
errors : [
3675
3668
"The 'handleNext1' function makes the dependencies of useEffect Hook " +
3676
- "(at line 15) change on every render. To fix this, move the 'handleNext1' " +
3677
- 'function inside the useEffect callback (at line 12). Alternatively, wrap the ' +
3678
- "'handleNext1' definition into its own useCallback() Hook." ,
3669
+ '(at line 15) change on every render. Move it inside the useEffect callback. ' +
3670
+ "Alternatively, wrap the 'handleNext1' definition into its own useCallback() Hook." ,
3679
3671
"The 'handleNext2' function makes the dependencies of useLayoutEffect Hook " +
3680
- "(at line 19) change on every render. To fix this, move the 'handleNext2' " +
3681
- 'function inside the useLayoutEffect callback (at line 16). Alternatively, wrap the ' +
3682
- "'handleNext2' definition into its own useCallback() Hook." ,
3672
+ '(at line 19) change on every render. Move it inside the useLayoutEffect callback. ' +
3673
+ "Alternatively, wrap the 'handleNext2' definition into its own useCallback() Hook." ,
3683
3674
"The 'handleNext3' function makes the dependencies of useMemo Hook " +
3684
- "(at line 23) change on every render. To fix this, move the 'handleNext3' " +
3685
- 'function inside the useMemo callback (at line 20). Alternatively, wrap the ' +
3686
- "'handleNext3' definition into its own useCallback() Hook." ,
3675
+ '(at line 23) change on every render. Move it inside the useMemo callback. ' +
3676
+ "Alternatively, wrap the 'handleNext3' definition into its own useCallback() Hook." ,
3687
3677
] ,
3688
3678
} ,
3689
3679
{
@@ -3907,8 +3897,7 @@ const tests = {
3907
3897
errors : [
3908
3898
`The 'handleNext' function makes the dependencies of ` +
3909
3899
`useEffect Hook (at line 14) change on every render. ` +
3910
- `To fix this, move the 'handleNext' function inside ` +
3911
- `the useEffect callback (at line 12). Alternatively, wrap the ` +
3900
+ `Move it inside the useEffect callback. Alternatively, wrap the ` +
3912
3901
`'handleNext' definition into its own useCallback() Hook.` ,
3913
3902
] ,
3914
3903
} ,
@@ -3944,9 +3933,8 @@ const tests = {
3944
3933
errors : [
3945
3934
"React Hook useEffect has a missing dependency: 'count'. " +
3946
3935
'Either include it or remove the dependency array. ' +
3947
- `If 'count' is only necessary for calculating the next state, ` +
3948
- `consider refactoring to the setCount(count => ...) form which ` +
3949
- `doesn't need to depend on the state from outside.` ,
3936
+ `You can also write 'setCount(count => ...)' if you ` +
3937
+ `only use 'count' for the 'setCount' call.` ,
3950
3938
] ,
3951
3939
} ,
3952
3940
{
@@ -3983,9 +3971,8 @@ const tests = {
3983
3971
errors : [
3984
3972
"React Hook useEffect has missing dependencies: 'count' and 'increment'. " +
3985
3973
'Either include them or remove the dependency array. ' +
3986
- `If 'count' is only necessary for calculating the next state, ` +
3987
- `consider refactoring to the setCount(count => ...) form which ` +
3988
- `doesn't need to depend on the state from outside.` ,
3974
+ `You can also write 'setCount(count => ...)' if you ` +
3975
+ `only use 'count' for the 'setCount' call.` ,
3989
3976
] ,
3990
3977
} ,
3991
3978
{
@@ -4022,9 +4009,8 @@ const tests = {
4022
4009
errors : [
4023
4010
"React Hook useEffect has a missing dependency: 'increment'. " +
4024
4011
'Either include it or remove the dependency array. ' +
4025
- `If 'increment' is only necessary for calculating the next state, ` +
4026
- `consider refactoring to the useReducer Hook. This ` +
4027
- `lets you move the calculation of next state outside the effect.` ,
4012
+ `You can also replace multiple useState variables with useReducer ` +
4013
+ `if 'setCount' needs the current value of 'increment'.` ,
4028
4014
] ,
4029
4015
} ,
4030
4016
{
@@ -4150,8 +4136,7 @@ const tests = {
4150
4136
` ,
4151
4137
errors : [
4152
4138
`The 'increment' function makes the dependencies of useEffect Hook ` +
4153
- `(at line 14) change on every render. To fix this, move the ` +
4154
- `'increment' function inside the useEffect callback (at line 9). ` +
4139
+ `(at line 14) change on every render. Move it inside the useEffect callback. ` +
4155
4140
`Alternatively, wrap the \'increment\' definition into its own ` +
4156
4141
`useCallback() Hook.` ,
4157
4142
] ,
@@ -4188,11 +4173,8 @@ const tests = {
4188
4173
errors : [
4189
4174
"React Hook useEffect has a missing dependency: 'increment'. " +
4190
4175
'Either include it or remove the dependency array. ' +
4191
- `If 'increment' is only necessary for calculating the next state, ` +
4192
- `consider refactoring to the useReducer Hook. This lets you move ` +
4193
- `the calculation of next state outside the effect. ` +
4194
- `You can then read 'increment' from the reducer ` +
4195
- `by putting it directly in your component.` ,
4176
+ 'You can also replace useState with an inline useReducer ' +
4177
+ `if 'setCount' needs the current value of 'increment'.` ,
4196
4178
] ,
4197
4179
} ,
4198
4180
{
@@ -4373,6 +4355,30 @@ const tests = {
4373
4355
`find the parent component that defines it and wrap that definition in useCallback.` ,
4374
4356
] ,
4375
4357
} ,
4358
+ {
4359
+ // The mistake here is that it was moved inside the effect
4360
+ // so it can't be referenced in the deps array.
4361
+ code : `
4362
+ function Thing() {
4363
+ useEffect(() => {
4364
+ const fetchData = async () => {};
4365
+ fetchData();
4366
+ }, [fetchData]);
4367
+ }
4368
+ ` ,
4369
+ output : `
4370
+ function Thing() {
4371
+ useEffect(() => {
4372
+ const fetchData = async () => {};
4373
+ fetchData();
4374
+ }, []);
4375
+ }
4376
+ ` ,
4377
+ errors : [
4378
+ `React Hook useEffect has an unnecessary dependency: 'fetchData'. ` +
4379
+ `Either exclude it or remove the dependency array.` ,
4380
+ ] ,
4381
+ } ,
4376
4382
] ,
4377
4383
} ;
4378
4384
0 commit comments