Passing a function during render to accesses ref causes compiler bailout - Ref values (the current
property) may not be accessed during render
#32
Replies: 1 comment
-
Howdy! I get something similar where I'm passing a ref to chidren as function. Playground here: https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCASggGYEC+BVMEGBAOiDAgIZx7sDcrAHaYc+IgTwBPLAgKUhAEwQwAkkKxRC9RszYgAdAHppssAOHCEADzGEqUIbzQQhBAEIQIAG25D1mngAFMDCBARwABZo3oqcQgA0wrTIRGER0bHxqQrKahpaADzEylRcUN54AMKRCHAA1giKqQBGXr5cbrQAfIJCtACUaW4RrmCEnDQAvCRklFSFABIAKgCyADIBWgCivhgIQnjdQUIV3gCEA33pnHiwblExcQdB6eGhI+HhpeWVNXWNZoEc7nNAiPBoLjeABqUKgCCSny+aCB3i4LQQ3gIAH4CEIuPsCABqfQAfXYxIIaIxWNS+P2iK+X1cNU6AHMEKkWZF2QhFp1FL4YIymQRJiLwrQJUQpekrslhCBaEA import { useRef } from "react";
import { type RenderInput } from "./types";
export function BooleanInput({
children,
}: {
children: RenderInput<{ defaultChecked: boolean }>;
}) {
const ref = useRef<HTMLInputElement>(null!);
return children(
{
defaultChecked: !!initialValue,
id: label ? name + "_" + label : name,
onChange: onChangeHandler,
ref,
},
{},
);
} The ref isn't being accessed here so the error isn't the most helpful. Is there a canonical alternative for children as function props being passed the ref? |
Beta Was this translation helpful? Give feedback.
-
Hi there! I have a component that passes a function for modal dismissal that checks a ref. This causes Compiler to throw an error:
InvalidReact: Ref values (the
currentproperty) may not be accessed during render. (https://react.dev/reference/react/useRef) (12:14)
https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCAwgIYA21ARpXANYA0JZASggGZukIBlPJTwICAXwLcYEDAQA6IGAkZ5FAbnkA7bdyha4eNBC0EAImjAZLYSnWoIAshAAmNABTE4ACzTUXylrIBIEuCDDObtTkJqJaeGASAJRE2gQEcCZghHCUWhZWNlzcBAC87AjF7tw0ZEnaaRlZhC6W1mCJ5fxUtAzM7u4ppQB8qabpBGgl7gCEufltRTwAdHCwgXgpyniwWpo64+kA9EcEACoA8mYXBK2FHTQE1BAA5mhwjeJsANrzBe1gYoAXSS+0a212IQQWjCEVcNBi8WhCU8jXSdwBLE+9S04gaWhO7EoLwQ+L0BiMJgIjgAnpEPClgOCEDsYKYADz-Gx2Bz06jDNFETy3RYdZJlUbs2l8xFxBIi+5gUrADE2SRHYZ48bso5ch72Jzw-naLXaEDiIA
I don't believe this violates the Rules of React (correct me if I'm wrong). Is there a good workaround for this issue, or is it a known compiler bug? I see facebook/react#30843 - is this a false positive?
Beta Was this translation helpful? Give feedback.
All reactions