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
detailMessage='This state value shadows a value passed as a prop.';
190
-
break;
191
-
case'fromPropsOrState':
192
-
detailMessage=
193
-
'This state value shadows a value passed as a prop or a value from state.';
194
-
break;
195
-
}
196
190
197
191
/*
198
192
* If we use a setState from an invalid useEffect elsewhere then we probably have to
@@ -204,15 +198,27 @@ function generateCompilerError(
204
198
error.type!=='fromState'
205
199
){
206
200
compilerDiagnostic=CompilerDiagnostic.create({
207
-
description: `${error.description} This state value shadows a value passed as a prop. Instead of shadowing the prop with local state, hoist the state to the parent component and update it there.`,
208
-
category: `Local state shadows parent state.`,
201
+
description: `The setState within a useEffect is deriving from ${error.description}. Instead of shadowing the prop with local state, hoist the state to the parent component and update it there. If you are purposefully initializing state with a prop, and want to update it when a prop changes, do so conditionally in render`,
202
+
category: `You might not need an effect. Local state shadows parent state.`,
209
203
severity: ErrorSeverity.InvalidReact,
210
204
}).withDetail({
211
205
kind: 'error',
212
206
loc: error.loc,
213
-
message: 'this setState synchronizes the state',
207
+
message: `this derives values from props ${error.type==='fromPropsOrState' ? 'and local state ' : ''}to synchronize state`,
@@ -234,8 +240,8 @@ function generateCompilerError(
234
240
}
235
241
}else{
236
242
compilerDiagnostic=CompilerDiagnostic.create({
237
-
description: `${error.description} Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.`,
238
-
category: `Derive values in render, not effects.`,
243
+
description: `${error.description ? error.description.charAt(0).toUpperCase()+error.description.slice(1) : ''}. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.`,
244
+
category: `You might not need an effect. Derive values in render, not effects.`,
239
245
severity: ErrorSeverity.InvalidReact,
240
246
}).withDetail({
241
247
kind: 'error',
@@ -270,7 +276,7 @@ function updateDerivationMetadata(
270
276
): void{
271
277
letnewValue: DerivationMetadata={
272
278
place: target,
273
-
sources: newSet(),
279
+
sources: [],
274
280
typeOfValue: typeOfValue??'ignored',
275
281
};
276
282
@@ -285,9 +291,9 @@ function updateDerivationMetadata(
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/useEffect/error.bug-derived-state-from-mixed-deps.expect.md
Error: You might not need an effect. Derive values in render, not effects.
38
38
39
-
This setState() appears to derive a value from both props and local state [prefix, name]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
39
+
Both props and local state [prefix, name]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/useEffect/error.derived-state-from-shadowed-props.expect.md
+21-3Lines changed: 21 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,19 +32,37 @@ function Component({props, number}) {
32
32
```
33
33
Found 1 error:
34
34
35
-
Error: Local state shadows parent state.
35
+
Error: You might not need an effect. Local state shadows parent state.
36
36
37
-
This setState() appears to derive a value from props [props, number]. This state value shadows a value passed as a prop. Instead of shadowing the prop with local state, hoist the state to the parent component and update it there.
37
+
The setState within a useEffect is deriving from props [props, number]. Instead of shadowing the prop with local state, hoist the state to the parent component and update it there. If you are purposefully initializing state with a prop, and want to update it when a prop changes, do so conditionally in render
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/useEffect/error.derived-state-with-conditional.expect.md
Error: You might not need an effect. Derive values in render, not effects.
36
36
37
-
This setState() appears to derive a value from props [value]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
37
+
Props [value]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
Copy file name to clipboardExpand all lines: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/useEffect/error.derived-state-with-side-effects.expect.md
Error: You might not need an effect. Derive values in render, not effects.
34
34
35
-
This setState() appears to derive a value from props [value]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
35
+
Props [value]. Derived values should be computed during render, rather than in effects. Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user.
0 commit comments