@@ -14,7 +14,31 @@ new MyRuleTester().run(name, rule, {
14
14
` ,
15
15
} ,
16
16
{
17
- name : "No-arg prop callback in response to internal state change" ,
17
+ name : "Pass live external state" ,
18
+ code : js `
19
+ const Child = ({ onFetched }) => {
20
+ const data = useSomeAPI();
21
+
22
+ useEffect(() => {
23
+ onFetched(data);
24
+ }, [onFetched, data]);
25
+ }
26
+ ` ,
27
+ } ,
28
+ {
29
+ // No idea why someone would do this, so we only check for passing state, but maybe there's a less contrived pattern.
30
+ // `no-manage-parent` would catch this anyway.
31
+ name : "Pass prop to parent" ,
32
+ code : js `
33
+ const Child = ({ text, onTextChanged }) => {
34
+ useEffect(() => {
35
+ onTextChanged(text);
36
+ }, [text, onTextChanged]);
37
+ }
38
+ ` ,
39
+ } ,
40
+ {
41
+ name : "No-arg prop callback" ,
18
42
code : js `
19
43
function Form({ onClose }) {
20
44
const [name, setName] = useState();
@@ -49,7 +73,7 @@ new MyRuleTester().run(name, rule, {
49
73
` ,
50
74
} ,
51
75
{
52
- name : "Prop from library HOC used internally " ,
76
+ name : "Pass internal state to HOC prop " ,
53
77
code : js `
54
78
import { withRouter } from 'react-router-dom';
55
79
@@ -63,7 +87,7 @@ new MyRuleTester().run(name, rule, {
63
87
` ,
64
88
} ,
65
89
{
66
- name : "Prop from library HOC used externally " ,
90
+ name : "Pass external state to HOC prop " ,
67
91
code : js `
68
92
import { withRouter } from 'react-router-dom';
69
93
@@ -72,7 +96,7 @@ new MyRuleTester().run(name, rule, {
72
96
73
97
useEffect(() => {
74
98
if (data.error) {
75
- history.push('/ error-page' );
99
+ history.push(data. error);
76
100
}
77
101
}, [data]);
78
102
});
@@ -81,14 +105,21 @@ new MyRuleTester().run(name, rule, {
81
105
] ,
82
106
invalid : [
83
107
{
84
- name : "Pass live internal fetched state" ,
108
+ name : "Pass live internal state" ,
85
109
code : js `
86
- const Child = ({ onFetched }) => {
87
- const [data, setData ] = useState();
110
+ const Child = ({ onTextChanged }) => {
111
+ const [text, setText ] = useState();
88
112
89
113
useEffect(() => {
90
- onFetched(data);
91
- }, [onFetched, data]);
114
+ onTextChanged(text);
115
+ }, [onTextChanged, text]);
116
+
117
+ return (
118
+ <input
119
+ type="text"
120
+ onChange={(e) => setText(e.target.value)}
121
+ />
122
+ );
92
123
}
93
124
` ,
94
125
errors : [
@@ -98,14 +129,15 @@ new MyRuleTester().run(name, rule, {
98
129
] ,
99
130
} ,
100
131
{
101
- name : "Pass live internal state" ,
132
+ name : "Pass live internal state AND external state " ,
102
133
code : js `
103
134
const Child = ({ onTextChanged }) => {
104
135
const [text, setText] = useState();
136
+ const data = useSomeAPI();
105
137
106
138
useEffect(() => {
107
- onTextChanged(text);
108
- }, [onTextChanged, text]);
139
+ onTextChanged(text, data );
140
+ }, [onTextChanged, text, data ]);
109
141
110
142
return (
111
143
<input
@@ -124,13 +156,20 @@ new MyRuleTester().run(name, rule, {
124
156
{
125
157
name : "Pass live derived internal state" ,
126
158
code : js `
127
- const Child = ({ onFetched }) => {
128
- const [data, setData ] = useState();
159
+ const Child = ({ onTextChanged }) => {
160
+ const [text, setText ] = useState();
129
161
130
162
useEffect(() => {
131
- const firstElement = data[0];
132
- onFetched(firstElement);
133
- }, [onFetched, data]);
163
+ const firstChar = text[0];
164
+ onTextChanged(firstChar);
165
+ }, [onTextChanged, text]);
166
+
167
+ return (
168
+ <input
169
+ type="text"
170
+ onChange={(e) => setText(e.target.value)}
171
+ />
172
+ );
134
173
}
135
174
` ,
136
175
errors : [
@@ -159,42 +198,7 @@ new MyRuleTester().run(name, rule, {
159
198
] ,
160
199
} ,
161
200
{
162
- name : "Pass live external state" ,
163
- code : js `
164
- const Child = ({ onFetched }) => {
165
- const data = useSomeAPI();
166
-
167
- useEffect(() => {
168
- onFetched(data);
169
- }, [onFetched, data]);
170
- }
171
- ` ,
172
- errors : [
173
- {
174
- messageId : messages . avoidPassingLiveStateToParent ,
175
- } ,
176
- ] ,
177
- } ,
178
- {
179
- name : "Pass derived live external state" ,
180
- code : js `
181
- const Child = ({ onFetched }) => {
182
- const data = useSomeAPI();
183
- const firstElement = data[0];
184
-
185
- useEffect(() => {
186
- onFetched(firstElement);
187
- }, [onFetched, firstElement]);
188
- }
189
- ` ,
190
- errors : [
191
- {
192
- messageId : messages . avoidPassingLiveStateToParent ,
193
- } ,
194
- ] ,
195
- } ,
196
- {
197
- name : "Pass final external state" ,
201
+ name : "Pass final internal state" ,
198
202
code : js `
199
203
function Form({ onSubmit }) {
200
204
const [name, setName] = useState();
@@ -224,23 +228,5 @@ new MyRuleTester().run(name, rule, {
224
228
} ,
225
229
] ,
226
230
} ,
227
- {
228
- name : "From props via member function" ,
229
- code : js `
230
- function DoubleList({ list }) {
231
- const [doubleList, setDoubleList] = useState([]);
232
-
233
- useEffect(() => {
234
- setDoubleList(list.concat(list));
235
- }, [list]);
236
- }
237
- ` ,
238
- errors : [
239
- {
240
- // We consider `list.concat` to essentially be a prop callback
241
- messageId : messages . avoidPassingLiveStateToParent ,
242
- } ,
243
- ] ,
244
- } ,
245
231
] ,
246
232
} ) ;
0 commit comments