Skip to content

[Compiler] Don't throw calculate in render if the blamed setter is used outside of the effect#148

Closed
everettbu wants to merge 5 commits into
mainfrom
pr34579
Closed

[Compiler] Don't throw calculate in render if the blamed setter is used outside of the effect#148
everettbu wants to merge 5 commits into
mainfrom
pr34579

Conversation

@everettbu

@everettbu everettbu commented Dec 12, 2025

Copy link
Copy Markdown

Mirror of facebook/react#34579
Original author: jorge-cab


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


Stack created with Sapling. Best reviewed with ReviewStack.

  • #34580
  • -> #34579
  • #34578
  • #34577
  • #34575
  • #34574

Summary:
This creates the test cases we expect this first iteration of calculate in render to catch

The goal is to have tests that will be in a good state once we have the first iteration of the calculate in render validation working, which should be pretty limited in what its capturing.

Test Plan:
Test cases
…k prop and local state derived values variables and add extra tests

Summary:
Biggest change of the stack, we track how values prop and local state values are derived throughout the entire component.

We are iterating over instructions instead of effects since some mutations can not be caught otherwise.

For every derivation we track the type of value its coming from (props or local state) and also the top most relevant sources (These would be the ones that are actually named instead of promoted like t0)

We propagate these relevant sources to each derivation.

This allows us to catch more complex useEffects though right now we are overcapturing some more complex cases which will be refined further up the stack.

This PR also adds a couple tests we will work towards fixing

Test Plan:
Added:
ref-conditional-in-effect-no-error
effect-contains-prop-function-call-no-error
derived-state-from-ref-and-state-no-error
… effect

Summary:
Using refs in an effect signify we are synchronizing with external state so to avoid overcapturing we just bail when we encounter one
…ction call in the effect

Summary:
Global function calls can introduce unexpected side effects, for this first iteration we are bailing out the validation when we encounter one.

Local function calls remain
…ed 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
@greptile-apps

greptile-apps Bot commented Dec 12, 2025

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR enhances the experimental validation for derived computations in effects by adding smarter detection of when setState functions are used outside the effect scope. The validation now tracks setState usages across the entire component and within effects separately, only throwing errors when the refactoring would be straightforward (i.e., when the setter isn't needed outside the effect for user interactions or passed as props).

Key changes:

  • Added setStateCache to track all setState usages across the component during the first pass
  • Added effectSetStateCache to track setState usages specifically within effect functions
  • Modified error condition to skip validation when setter has external usages, avoiding false positives for legitimate patterns like controlled inputs
  • Comprehensive test coverage for the new no-error cases (setter in onChange, setter passed as prop, global function calls)

Confidence Score: 4/5

  • This PR is safe to merge with minor considerations about the validation logic complexity
  • The implementation correctly adds tracking for setState usages and has comprehensive test coverage. The logic appears sound based on test cases, though the condition effectSetStateCache.length === setStateCache.length - 1 is somewhat opaque and would benefit from a comment explaining when this evaluates to true
  • ValidateNoDerivedComputationsInEffects_exp.ts could use additional inline documentation explaining the setState tracking logic and the specific condition being checked

Important Files Changed

File Analysis

Filename Score Overview
compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts 4/5 New experimental validation adds setState tracking logic to avoid false positives when setters are used outside effects
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts 5/5 Added experimental validation hook with config flag
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts 5/5 Added config flag for experimental validation

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

35 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@everettbu everettbu added the Resolution: Stale Automatically closed due to inactivity label Jan 19, 2026
@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Jan 26, 2026
@everettbu
everettbu deleted the pr34579 branch January 26, 2026 23:16
@everettbu
everettbu restored the pr34579 branch January 27, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants