-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Misleading error description when using wrong useRef #14980
Comments
Runnable example please? Such as CodeSandbox. |
@gaearon Just updated the post. |
ref="nameRef" => ref={nameRef} |
@huubvKaopiz He already knows how to fix it. This error looks misleading to me, too. |
Huh, it looks like the issue shows up even without useRef, by simply using a string ref on |
What about an error message like "Function components cannot have string refs. Did you mean to use React.useRef()? |
To be clear, this would only be if a function component uses a string ref. i think the initial error still has use |
@threepointone I experienced this today as well when trying to conditionally pass a function MyApp() {
const ref = React.useRef();
const someCondition = false;
return <div ref={someCondition && ref} />
} I believe React is right on throwing the error, but the message is indeed a bit misleading. Would it be a possibility to exclude/handle boolean types from react/packages/react-reconciler/src/ReactChildFiber.js Lines 112 to 116 in 54f6673
|
I do agree in which the error is misleading or outdated. Prior to hooks, using refs wasn't possible? So, this error isn't about "Function components cannot have refs", but rather there's an invalid configuration? |
…ve refs. Did you mean to use React.forwardRef()? making ref to userref https://codesandbox.io/s/misleading-ref-error-rc2ed facebook/react#14980
This issue seems to be solved by #18031 |
Closed by #18031 |
There's a misleading error when using wrong the
useRef
hook.See the example here https://codesandbox.io/s/yvl21qm62j
The code above leads to this error
Function components cannot have refs. Did you mean to use React.forwardRef()?
. I'm moving some class components to Hooks, and stumbled upon this, and it took me a while due to the wrong error message.The text was updated successfully, but these errors were encountered: