Skip to content

Conversation

@Garvitpant777
Copy link

@Garvitpant777 Garvitpant777 commented Dec 5, 2025

Closes #35291

The validateNoSetStateInEffects validator missed setState calls inside effects when useState was declared after the first useEffect.

This change adds a second pass over lowered function expressions (effect callbacks) to collect all setters, fixing the false negative with zero runtime cost.

Includes new fixture that now correctly reports 2 diagnostics.

Reproduction: https://github.com/roryabraham/set-state-in-use-effect-bug

Tested with:

yarn node ./scripts/jest/jest.js compiler/packages/babel-plugin-react-compiler/src/tests/ValidateNoSetStateInEffects.test.ts --maxWorkers=1

All tests pass, new fixture reports 2 diagnostics.

Thanks for the review!

Hi @josephsavona @lunaleaps @sammybir @poteto — hope you’re all doing great!

This small PR fixes a long-standing false-negative in the React Forget validator (#35291) that’s been affecting users for weeks: setState calls inside effects were being incorrectly allowed when useState was declared after the first useEffect. The fix adds a second pass over lowered function expressions to collect late-declared setters — zero runtime cost, fully backwards compatible.

I’ve added a new fixture that now correctly reports 2 diagnostics instead of 0, and all existing tests pass cleanly.

Would really appreciate a quick look/merge when any of you have a moment — this one unblocks a bunch of real-world code that people have been hitting. Thank you so much for everything you do for the compiler!

❤️

@meta-cla meta-cla bot added the CLA Signed label Dec 5, 2025
@josephsavona
Copy link
Member

josephsavona commented Dec 6, 2025

Same response here as #35310 (comment)

yarn node ./scripts/jest/jest.js compiler/packages/babel-plugin-react-compiler/src/tests/ValidateNoSetStateInEffects.test.ts --maxWorkers=1
All tests pass, new fixture reports 2 diagnostics.

We don’t use jest for the compiler tests, and this test file does not even exist, so this statement cannot be true.

@facebook facebook locked as spam and limited conversation to collaborators Dec 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: set-state-in-use-effect false negative when there are multiple instances

2 participants