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]: Mutating a ref returned from a function warns #29196
When consuming mutable refs from custom hooks (or via props), the consuming components should be allowed to mutate them inside effects/event handlers.
functionuseMyRef(){returnuseRef('test');}functionHello(){constref=useMyRef();useEffect(()=>{ref.current='next';// ~~~ InvalidReact: Mutating a value returned from a function whose return value should not be mutated. Found mutation of `ref` (8:8)});}
How often does this bug happen?
Every time
What version of React are you using?
19
Reacted by Cory House, Olivier Tassinari, Huynh Duc Duy, Marin Atanasov, Jakub Hromek and Alexander Harding
Thanks for posting! The cause is very similar to #29160 (comment) — the compiler currently assumes that only values that come directly from useRef() are refs. As with #29160 , we will likely need to explore a heuristic for understanding which values may be refs, based on the ecosystem convention of "ref" or "-Ref".
The error you're encountering happens because mutating a prop or a hook argument directly inside a hook is not allowed in React. The error arises because the target ref passed to the useResizeHandle hook is being directly mutated, which React warns against.
To resolve this, you should avoid directly modifying the target ref and instead use a local variable to track changes or update the target element in a way that doesn't involve directly mutating the passed reference.
Modified one:
`
import * as React from 'react';
export function useResizeHandle(
target: React.MutableRefObject<HTMLDivElement | null>,
) {
const bar = React.useRef(null);
React.useEffect(() => {
function resizeObject(event: MouseEvent | TouchEvent) {
if (bar.current && target.current) {
bar.current.style.width = "10px";
target.current.style.width = "10px"; // Make sure you are not mutating target itself but the element it refers to
}
}
}, [target]); // target ref is a dependency here, but we don't mutate it directly
return {
dragging: true,
};
}`
Key Changes on the above code:
Avoid Direct Mutation instead of directly mutating target or target.current, the code now modifies the style properties of the element referred to by target.current, which is allowed.
Check for Existence and Added a check to ensure that bar.current and target.current are not null before accessing their properties.
Note:
1.Make sure that you are not using React version 19, as there is no such version at the time of writing. The latest stable version is React 18. You might want to confirm which version you are using.
2.Ensure that ESLint is configured correctly with appropriate rules to avoid these kinds of errors in the future.
Reacted by Doesntmeananything, Alexander Arvidsson, Alexander Harding, jiaSheng, Burak Güner, Yuri Paoloni, Wojciech Dróżdż, Jacob Cutshall and lbland94
Activity
josephsavona commentedon May 30, 2024
Thanks for posting! The cause is very similar to #29160 (comment) — the compiler currently assumes that only values that come directly from
useRef()are refs. As with #29160 , we will likely need to explore a heuristic for understanding which values may be refs, based on the ecosystem convention of "ref" or "-Ref".gsathya commentedon Jun 3, 2024
I'm looking into adding this. Assigning this to myself
[compiler] Treat ref-like named objects as refs
[compiler] Treat ref-like named objects as refs
[compiler] Treat ref-like named objects as refs (#29916)
maddymathan commentedon Aug 12, 2024
The error you're encountering happens because mutating a prop or a hook argument directly inside a hook is not allowed in React. The error arises because the target ref passed to the useResizeHandle hook is being directly mutated, which React warns against.
To resolve this, you should avoid directly modifying the target ref and instead use a local variable to track changes or update the target element in a way that doesn't involve directly mutating the passed reference.
Modified one:
`
import * as React from 'react';
export function useResizeHandle(
target: React.MutableRefObject<HTMLDivElement | null>,
) {
const bar = React.useRef(null);
React.useEffect(() => {
function resizeObject(event: MouseEvent | TouchEvent) {
if (bar.current && target.current) {
bar.current.style.width = "10px";
target.current.style.width = "10px"; // Make sure you are not mutating target itself but the element it refers to
}
}
}, [target]); // target ref is a dependency here, but we don't mutate it directly
return {
dragging: true,
};
}`
Key Changes on the above code:
Avoid Direct Mutation instead of directly mutating target or target.current, the code now modifies the style properties of the element referred to by target.current, which is allowed.
Check for Existence and Added a check to ensure that bar.current and target.current are not null before accessing their properties.
Note:
1.Make sure that you are not using React version 19, as there is no such version at the time of writing. The latest stable version is React 18. You might want to confirm which version you are using.
2.Ensure that ESLint is configured correctly with appropriate rules to avoid these kinds of errors in the future.
aeharding commentedon Oct 30, 2024
Here's another variant of this issue - passing ref as a prop to a child component causes Compiler to not recognize it as a ref.
https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEACgIYwKY4AUAlEcADrFFyFg5EAOZYYCACYAlBGiIBeIlAGi0NNGQA2AuixZEilHLGIAeAMIALPEsE8+AkWInBe-IXIC+RAPQA+Fk-WYM2fIRExqaCNMAWDtbiTgzMrOyYnESEADKEAOYklPySRPSS7owamhFWcgB0cLCU1Lk4MFAIANzF3pjFMghpmJnZYDSpGVkI-HQtmE4gTkA
If you set
useRefdirectly in<Child />it works as expected:https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEACgIYwKY4AUAlEcADrFFyFg5EAOZYYCACYAlBGiIBeIlAGi0NNGQA2AuixZEilHLGIAeAMIALPEsE8+AkWInBe-IXIC+RAPQA+Fk-WYM2fIRExqaCNMAWDtZoyEQA+kRODMys7JicEVZyktKyYgrKqj6aqemEADKEAOYklPzZ9JLujBqaGY5iAHRwsJTU2TgwUAgA3C3emC0yCBWY1bVgNOVVNQj8dKOYTiBOQA
poteto commentedon Nov 13, 2024
This should be closed by #29916 - thanks @gsathya for fixing!
[compiler] Treat ref-like named objects as refs (react#29916)