@@ -13,6 +13,30 @@ new MyRuleTester().run(name, rule, {
13
13
}
14
14
` ,
15
15
} ,
16
+ {
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
+ } ,
16
40
{
17
41
name : "No-arg prop callback in response to internal state change" ,
18
42
code : js `
@@ -80,23 +104,6 @@ new MyRuleTester().run(name, rule, {
80
104
} ,
81
105
] ,
82
106
invalid : [
83
- {
84
- name : "Pass live internal fetched state" ,
85
- code : js `
86
- const Child = ({ onFetched }) => {
87
- const [data, setData] = useState();
88
-
89
- useEffect(() => {
90
- onFetched(data);
91
- }, [onFetched, data]);
92
- }
93
- ` ,
94
- errors : [
95
- {
96
- messageId : messages . avoidPassingLiveStateToParent ,
97
- } ,
98
- ] ,
99
- } ,
100
107
{
101
108
name : "Pass live internal state" ,
102
109
code : js `
@@ -124,13 +131,20 @@ new MyRuleTester().run(name, rule, {
124
131
{
125
132
name : "Pass live derived internal state" ,
126
133
code : js `
127
- const Child = ({ onFetched }) => {
128
- const [data, setData ] = useState();
134
+ const Child = ({ onTextChanged }) => {
135
+ const [text, setText ] = useState();
129
136
130
137
useEffect(() => {
131
- const firstElement = data[0];
132
- onFetched(firstElement);
133
- }, [onFetched, data]);
138
+ const firstChar = text[0];
139
+ onTextChanged(firstChar);
140
+ }, [onTextChanged, text]);
141
+
142
+ return (
143
+ <input
144
+ type="text"
145
+ onChange={(e) => setText(e.target.value)}
146
+ />
147
+ );
134
148
}
135
149
` ,
136
150
errors : [
@@ -159,42 +173,7 @@ new MyRuleTester().run(name, rule, {
159
173
] ,
160
174
} ,
161
175
{
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" ,
176
+ name : "Pass final internal state" ,
198
177
code : js `
199
178
function Form({ onSubmit }) {
200
179
const [name, setName] = useState();
@@ -224,23 +203,5 @@ new MyRuleTester().run(name, rule, {
224
203
} ,
225
204
] ,
226
205
} ,
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
206
] ,
246
207
} ) ;
0 commit comments