question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receiving "Function components cannot be given refs." warning

See original GitHub issue

I’m changing a component that was being wrapped by MenuProvider to a function component, and am getting this warning.

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

It still seems to work, however.

I didn’t see an existing issue for it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
jmcacommented, Mar 16, 2020

You could use a fragment in a pinch, that’s what I’m currently doing:

<MenuProvider>
    <>
        <SomeFunctionalComp />
    </>
</MenuProvider>
6reactions
renato-bohlercommented, Mar 30, 2020

Apparently setting the storeRef prop to false also removes the warning.

// No warnings :)
<MenuProvider id={event.model.options.id} storeRef={false}>
  <Widget engine={this.engine} model={event.model} />
</MenuProvider>

// Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
<MenuProvider id={event.model.options.id}>
  <Widget engine={this.engine} model={event.model} />
</MenuProvider>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I avoid 'Function components cannot be given refs ...
Warning : Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?.
Read more >
Solved: Function components cannot be given refs ... - GitHub
A functional component isn't backed by an "instance" of anything, so it can't handle ref's passed to it automatically. BUT, if you wrap...
Read more >
Forwarding Refs - React
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >
ref prop should not be used on React function components
This rule applies when a ref prop is specified on a React function component. The ref prop is used to provide a reference...
Read more >
next link function components cannot be given refs. attempts ...
9. As the warning states you cannot assign refs to functional components without the usage of forwardRef. In order to have access to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found