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.

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

See original GitHub issue

After update to latest tippy i’ve started to get this error. Reproduction case: Work’s fine:

          <Tooltip content={'ам'}>
              <div>1</div>
          </Tooltip>

Got error:

          <Tooltip content={'ам'} >
                <Icon />
          </Tooltip>

or

          <Tooltip content={'ам'} >
            1
          </Tooltip>

Icon code:

export const Icon = props => (
  <svg {...props}>
     // svg image code
  </svg>
)

Tooltip code:

import Tippy from '@tippy.js/react'
import React, { useEffect, useState } from 'react'

export function Tooltip({ children, sharedContent, content, pure, ...rest }) {
  const [tip, tipSet] = useState(false)

  useEffect(() => {
    if (sharedContent) {
      switch (sharedContent) {
        case 'something':
          content = (
            <div>
               shared tips
            </div>
          )
      }
    }
    tipSet(content)
  }, [content])

  if (!tip) return false

  return (
    <Tippy content={tip} {...rest}>
      {pure
        ? React.cloneElement(children)
        : React.cloneElement(children, { className: 'tippy-target' })}  // change cursor to one with question mark
    </Tippy>
  )
}

Tooltip.defaultProps = {
  ignoreAttributes: true,
  animateFill: false,
  delay: [300, 0],
  interactive: true,
  animation: 'scale',
  theme: 'knigAm',
  duration: [300, 300],
  distance: 5,
  maxWidth: '330px',
  sticky: true,
  touchHold: true,
  a11y: false
}

Any thoughts to try?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

27reactions
atomikscommented, Mar 6, 2019

I think a lot of people are ending up on this issue (if GitHub search is accurate!)

The full solution, including a workaround for legacy components, is available on the README: https://github.com/atomiks/tippy.js-react#component-children

25reactions
atomikscommented, Feb 12, 2019

You need to use forwardRef if you’re using a component as a child now. The internals were changed to remove findDOMNode which required this change

Edit: the docs are definitely missing this though, I forgot. I will add it in now 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I avoid 'Function components cannot be given refs ...
Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Check the render method of ForwardRef . in NavLink (created...
Read more >
Solved: Function components cannot be given refs ... - GitHub
Solved: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? #2120.
Read more >
ref prop should not be used on React function components
React Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Refs ......
Read more >
warning: function components cannot be given refs. attempts ...
Iget error 'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?' . How...
Read more >
Forwarding Refs - React
The second ref argument only exists when you define a component with React.forwardRef call. Regular function or class components don't receive the ref...
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