[compiler] Check ref access in useState initializer function #32506
+64
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes #31418 .
The compiler did not throw an error when a useState initializer function accessed a ref value during render.
How did you test this change?
I added new tests using useState initializer functions to the babel-plugin-react-compiler and eslint-plugin-react-compiler packages and ran
yarn test
.I also built the compilers and checked they worked in a small project with babel and the vs code eslint plugin.

The babel-plugin-react-compiler logged an error due to the ref access:
The eslint-plugin-react-compiler also reported an error:
