You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Compiler Bug]: eslint-plugin-react-compiler errors when updating initialization of ref.current #30782
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
Reacted by Jonson Petard, SharkEzz, zamarawka, Tamás Hámos, Bas van den Wollenberg, Maja Cieślak and Jonghyeon Ko
i also run into the problem in a different scenario:
when passing the ref object to a custom hook the compiler is not optimizing my code. if i remove the ref access it works as expected.
// pass the ref object to a custom hook and it is preventing the optimizations.
const mergedRefs = useMergedRef<HTMLDivElement>(ref, setPopperElement);
This works against latest experimental release (0.0.0-experimental-4e0eccf-20240830). It probably breaks the rules, but so far didn't encounter serious issues.
The linter rule complains only about that .current in the return statement. However, the conditional needs to have exactly this form of fooRef.current === null, otherwise the rule will report a false positive.
Activity
jeremy-code commentedon Aug 21, 2024
Related: #30716
parthnegi21 commentedon Aug 28, 2024
Try this
import { useRef, useState, useEffect } from "react";
const UNINITIALIZED = Symbol("UNINITIALIZED");
export const useOnce = <T,>(initialValue: T | (() => T)) => {
const ref = useRef < T | typeof UNINITIALIZED > (UNINITIALIZED);
const [value, setValue] = useState < T | typeof UNINITIALIZED > (UNINITIALIZED);
useEffect(() => {
if (ref.current === UNINITIALIZED) {
const resolvedValue = typeof initialValue === "function" ? initialValue() : initialValue;
ref.current = resolvedValue;
setValue(resolvedValue);
} else {
setValue(ref.current);
}
}, [initialValue]);
return value;
};
export default function MyApp() {
return
}
State Initialization: Added a state value to store the initialized value.
useEffect: The ref is now initialized inside the useEffect hook, which sets the value both in the ref and in the state.
Return Value: The hook returns the state value, ensuring that the initial render doesn't access ref.current
josephsavona commentedon Aug 31, 2024
Thanks for posting. This is a known limitation of the new linter.
hlege commentedon Sep 1, 2024
i also run into the problem in a different scenario:
when passing the ref object to a custom hook the compiler is not optimizing my code. if i remove the
refaccess it works as expected.iahu commentedon Sep 2, 2024
Do you deprecated to use
useRef? if not, how to use it?alisherks commentedon Sep 2, 2024
Is there any available workaround for this? This limitation also affects the useImperativeHandle.
alisherks commentedon Sep 2, 2024
To anyone encountering optimization issues with the usage of
useImperativeHandle, you can place theforwardedRefinside auseStatehook.This works against latest experimental release (0.0.0-experimental-4e0eccf-20240830). It probably breaks the rules, but so far didn't encounter serious issues.
nkalpakis21 commentedon Sep 5, 2024
ravicious commentedon Oct 22, 2024
I think this is addressed in
19.0.0-beta-8a03594-20241020. I have a hook that looks like this:The linter rule complains only about that
.currentin the return statement. However, the conditional needs to have exactly this form offooRef.current === null, otherwise the rule will report a false positive.Fix ref.current error during render initialization
Fix ref.current error during render initialization
[compiler] ref guards apply up to fallthrough of the test
[compiler] ref guards apply up to fallthrough of the test
[compiler] ref guards apply up to fallthrough of the test
[compiler] ref guards apply up to fallthrough of the test
[compiler] ref guards apply up to fallthrough of the test
[compiler] ref guards apply up to fallthrough of the test (#34024)
[compiler] ref guards apply up to fallthrough of the test (react#34024)