gilbarbara / react-floater

Advanced tooltips for React
https://codesandbox.io/s/github/gilbarbara/react-floater/tree/main/demo
MIT License
220 stars 37 forks source link

add support for refs #12

Closed gilbarbara closed 6 years ago

gilbarbara commented 6 years ago

The problem here is that ref is only set after the first parent render, so it doesn't exist when the Tooltip mounts.

gilbarbara commented 6 years ago

I've found a way around this but I'm sure if it's the best approach. You need to force a new render after the ref is set in order to use it for the Tooltip.

export default class MyComponent extends React.PureComponent {
  setRef = (c) => {
    this.target = c;
    this.forceUpdate();
  };

  render() {
    return (
      <div>
        <Target innerRef={this.setRef} />
        {this.target && (
          <Tooltip
            content="My content"
            target={this.target}
          >
            <span>Click me</span>
          </Tooltip>
        )}
      </div>
    );
  }
}