Skip to content
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

Closed
madroneropaulo opened this issue Feb 28, 2019 · 11 comments
Closed

Misleading error description when using wrong useRef #14980

madroneropaulo opened this issue Feb 28, 2019 · 11 comments

Comments

@madroneropaulo
Copy link

madroneropaulo commented Feb 28, 2019

There's a misleading error when using wrong the useRef hook.

function MyComponent() {
    let nameRef = useRef();

   return (
      <div>
        <input ref="nameRef" type="text" />       
    </div>
  );
}

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.

@gaearon
Copy link
Collaborator

gaearon commented Feb 28, 2019

Runnable example please? Such as CodeSandbox.

@madroneropaulo
Copy link
Author

@gaearon Just updated the post.

@huubvKaopiz
Copy link

ref="nameRef" => ref={nameRef}

@tiendo1011
Copy link

@huubvKaopiz He already knows how to fix it. This error looks misleading to me, too.

@threepointone
Copy link
Contributor

Huh, it looks like the issue shows up even without useRef, by simply using a string ref on <input/>. This looks like an actual bug.

@natejcho
Copy link

What about an error message like "Function components cannot have string refs. Did you mean to use React.useRef()?

@natejcho
Copy link

To be clear, this would only be if a function component uses a string ref.

i think the initial error still has use

@klujanrosas
Copy link

klujanrosas commented Nov 21, 2019

@threepointone I experienced this today as well when trying to conditionally pass a ref like

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 coerceRef here?

if (
mixedRef !== null &&
typeof mixedRef !== 'function' &&
typeof mixedRef !== 'object'
) {

Repro:
https://codesandbox.io/s/misleading-ref-error-rc2ed

@lynellf
Copy link

lynellf commented Dec 31, 2019

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?

sahaanish26 added a commit to sahaanish26/quickenmeal that referenced this issue Mar 31, 2020
@okmttdhr
Copy link
Contributor

This issue seems to be solved by #18031

@eps1lon
Copy link
Collaborator

eps1lon commented Apr 29, 2022

Closed by #18031

@eps1lon eps1lon closed this as completed Apr 29, 2022
@eps1lon eps1lon reopened this Apr 29, 2022
@eps1lon eps1lon closed this as completed Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants