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.

[Component] Custom Icons give console error and do not allow tooltips.

See original GitHub issue

I am trying to load icons from React-Icons, and when i use <Icon as={MdPeople} /> for example I get the following error in console: (I tried it with <Box /> with the same result)

Warning: 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 `Context.Consumer`.
    in MdPeople (created by Context.Consumer)
    in Styled(div) (created by Icon)
    in Icon (at PartnerIcon.tsx:20)
    in Tooltip (at PartnerIcon.tsx:14)
    ...

My componnet looks like so:

import React from 'react'
import { Team } from 'types/Team'
import { MdPeople } from 'react-icons/md'
import { Tooltip, Icon } from '@chakra-ui/core'

type Props = {
  team: Team
}

export const PartnerIcon = ({ team }: Props) => {
  // use user to determine if this should be rendered.

  return (
    <Tooltip
      label={`Partner ${team.name}`}
      aria-label="Partner"
      placement="top"
      hasArrow
    >
      <Icon color="white" as={MdPeople} /> // tried this with <Box as... /> with the same result.
    </Tooltip>
  )
}

On top of this error, It does not show a tooltip when I hover over the icon. - I want to point out that the tooltip works fine when used with a normal icon.

Not exactly sure how to debug this.

Oh also, if you wrap a <Tooltip> element around this example you can see that the tooltip does not render: https://chakra-ui.com/icon#adding-custom-icons

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

13reactions
luizcieslakcommented, Jul 22, 2020

I’m facing the same issue. While react-icons doesn’t support forwardRef, a quick solution would be to add the icon as a child:

<Tooltip isOpen label="hello">
  <Box size="32px" color="green.400">
    <MdSettings />
  </Box>
</Tooltip>
5reactions
segunadebayocommented, Jul 8, 2020

This issue is happening because react-icons doesn’t use forwardRef and tooltip can’t work correctly without ref placed in the icon.

See here: https://github.com/react-icons/react-icons/blob/f95cf84f7beff3638be8bb12a51d00338da97100/packages/react-icons/src/iconBase.tsx#L30

@with-heart I’ll suggest we open an issue or PR in react-icons repo to get this fixed.

I believe it works well if you use any icon from @chakra-ui/icons

Thanks guys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material UI tooltip doesn't display on custom component ...
The following can hold a ref: Any Material-UI component; class components i.e. React.Component or React.PureComponent; DOM (or host) components ...
Read more >
4 Ways to Show Tooltips in React with react-tooltip, Material UI ...
This is done with the data-tip and data-for custom attributes. We specify these attributes for the <button> element, but you can display  ......
Read more >
How To Make an Extremely Reusable Tooltip Component ...
All you need to do is import the Tooltip component and use it as a wrapper. Make it go above anything you want...
Read more >
Components - Bootstrap
Icon classes should only be used on elements that contain no text content and have no child ... Tooltips & popovers in button...
Read more >
Create a custom tooltip component in Vue - LogRocket Blog
vue create tooltips. Let's delete the default components in our new project and make sure that nothing renders in the browser.
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