-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Closed
Labels
Compiler: Ref ValidationComponent: React CompilerStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bugType: Bug
Description
What kind of issue is this?
- React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
- babel-plugin-react-compiler (build issue installing or using the Babel plugin)
- eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
- react-compiler-healthcheck (build issue installing or using the healthcheck script)
Link to repro
Repro steps
- Initialize
useRef
with some dummy value (e.g. null, Symbol, undefined, etc.) to be changed after initialization/during render. - Update
ref.current
by checking whether it is equivalent to its initial condition (as per documentation: useRef#Avoiding recreating the ref contents)
-
To solve it, you may initialize the ref like this instead:
function Video() {
const playerRef = useRef(null);
if (playerRef.current === null) {
playerRef.current = new VideoPlayer();
}
// ...
-
Normally, writing or reading ref.current during render is not allowed. However, it’s fine in this case because the result is always the same, and the condition only executes during initialization so it’s fully predictable.
- eslint-plugin-react-compiler gives error
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef)
How often does this bug happen?
Every time
What version of React are you using?
19.0.0-rc-1d989965-20240821
greenhat616, SharkEzz, zamarawka, hlege, brw and 2 moredconco, hlege and manudeli
Metadata
Metadata
Assignees
Labels
Compiler: Ref ValidationComponent: React CompilerStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bugType: Bug