react-tooltip icon indicating copy to clipboard operation
react-tooltip copied to clipboard

Can't access .rebuild() static method when using functional components

Open NathZ1 opened this issue 4 years ago • 2 comments

Hi everyone,

I am struggling to refresh tooltips with my conditional content. I know i need to rebuild after the content is updated, but I can't access the .rebuild() method...

In the past with functional components I would create a reference to the component to access its methods, but this does not work in this case. Please see below example -

import { useState, useRef } from "react" 
import ReactTooltip from 'react-tooltip'

const Component1 = () => {
  return <p key='component1' data-tip='Test tool-tip 1'>hover over me 1</p>
}

const Component2 = () => {
  return <p key='component2' data-tip='Test tool-tip 2'>hover over me 2</p>
}

//will show tool-tip on first load, but not after the renderComponent is toggled off then on again
export default function Main() {
  const [ currentComponent, setComponent ] = useState<JSX.Element>(Component1)
  const [ renderComponent, setRenderComponent ] = useState<boolean>(true)
  const tooltipComponentRef = useRef<ReactTooltip>(null) 

  function cycleComponentClick() {
    if (currentComponent.props.children === "hover over me 1") {
      setComponent(Component2)
    }
    else {
      setComponent(Component1)
    }
  }

  function toggleRenderComponent() {
    setRenderComponent(!renderComponent)
  }

  //tooltipComponentRef.current?.rebuild() //gives error: "Property 'rebuild' does not exist on type 'ReactTooltip'..."

  return (
    <div>
      <button onClick={cycleComponentClick}>Cycle Component</button>
      <button onClick={toggleRenderComponent}>Toggle Render Component</button>
      {renderComponent ? currentComponent : null}
      <ReactTooltip ref={tooltipComponentRef} />
    </div>
  );
}

Is this considered a bug or is there a work-around to help with the issue?

Thanks in advance

NathZ1 avatar Jun 30 '21 02:06 NathZ1

It seems you should use data-for attribute for dom node (your component) and dynamically change ID prop for ReactTooltip component then it will match data-for === id and will re render when you hover for different components. It is not unusual case tho

stevenKirill avatar Nov 09 '21 07:11 stevenKirill

It happened to me also. In readme there is an information about that.

  1. Import node_modules/react-tooltip/dist/index.js into your page.
  2. Make sure react and react-dom into your page.

You can read whole documentation here

berkaygurbuz avatar Mar 31 '22 13:03 berkaygurbuz