Skip to content

Commit e76a723

Browse files
committed
[Compiler] Don't throw calculate in render if the blamed setter is used outside of the effect
Summary: If the setter is used both inside and outside the effect then usually the solution is more complex and requires hoisting state up to a parent component since we can't just remove the local state. To do this, we now have 2 caches that track setState usages (not just calls) since if the effect is passed as an argument or called outside the effect the solution gets more complex which we are trying to avoid for now
1 parent 9d0c440 commit e76a723

File tree

36 files changed

+841
-458
lines changed

36 files changed

+841
-458
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import {validateNoFreezingKnownMutableFunctions} from '../Validation/ValidateNoF
103103
import {inferMutationAliasingEffects} from '../Inference/InferMutationAliasingEffects';
104104
import {inferMutationAliasingRanges} from '../Inference/InferMutationAliasingRanges';
105105
import {validateNoDerivedComputationsInEffects} from '../Validation/ValidateNoDerivedComputationsInEffects';
106+
import {validateNoDerivedComputationsInEffects_exp} from '../Validation/ValidateNoDerivedComputationsInEffects_exp';
106107
import {nameAnonymousFunctions} from '../Transform/NameAnonymousFunctions';
107108

108109
export type CompilerPipelineValue =
@@ -275,6 +276,10 @@ function runWithEnvironment(
275276
validateNoDerivedComputationsInEffects(hir);
276277
}
277278

279+
if (env.config.validateNoDerivedComputationsInEffects_exp) {
280+
validateNoDerivedComputationsInEffects_exp(hir);
281+
}
282+
278283
if (env.config.validateNoSetStateInEffects) {
279284
env.logErrors(validateNoSetStateInEffects(hir, env));
280285
}

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ export const EnvironmentConfigSchema = z.object({
334334
*/
335335
validateNoDerivedComputationsInEffects: z.boolean().default(false),
336336

337+
/**
338+
* Experimental: Validates that effects are not used to calculate derived data which could instead be computed
339+
* during render. Generates a custom error message for each type of violation.
340+
*/
341+
validateNoDerivedComputationsInEffects_exp: z.boolean().default(false),
342+
337343
/**
338344
* Validates against creating JSX within a try block and recommends using an error boundary
339345
* instead.

0 commit comments

Comments
 (0)