React Bootstrap hide tooltip
Asked Answered
F

2

6

Is there a way to hide the OverlayTrigger/Tooltip element by default? eg. overlay={this.state.show ? <Tooltip>showing</Tooltip> : null} works but throws a warning on console:

The prop overlay is marked as required in OverlayTrigger, but its value is null

Would this be the only way?

{!this.state.show ? {component} :
 <OverlayTrigger ...>
   {component}
 </OverlayTrigger>
}
Fabiolafabiolas answered 7/9, 2017 at 7:41 Comment(5)
You are closing your curly-braces too early. Try: overlay={this.state.show ? <Tooltip>showing</Tooltip> : null}.Embolic
@Embolic minor typo. The corrected version still throws "The prop overlay is marked as required in OverlayTrigger, but its value is null"Fabiolafabiolas
@Avery235: try an empty string instead of nullTimbal
@FiriceNguyen with '' it shows a tooltip with '', with nothing at all it shows Cannot read property 'props' of null at OverlayTrigger.renderFabiolafabiolas
An empty string doesn't work, it still throws an error.Kiele
E
4

The OverlayTrigger component must have a overlay prop passed. If you don't want the tooltip, you also don't want an overlay to trigger. Hence, you'd want to remove it if this.state.show is falsy.

{this.state.show 
  ? <OverlayTrigger overlay={<Tooltip>showing</Tooltip>}>
      <button>Click me!</button>
    </OverlayTrigger>
  : <button>Click me!</button>
}

Edit: Yes, the code in your update would be the way to do it.

Embolic answered 7/9, 2017 at 7:47 Comment(3)
This doesn't help when instead of a simple button you have some content you want to display, but don't have any tooltip for; you'd have to duplicate more than just a button with some text. It should be possible to have an empty overlay with react-bootstrap simply displaying the content without a tooltip. As a workaround, I had to add a class to hide the tooltip in the overlay when I have nothing to show, which is just plain silly.Kiele
This worked perfectly for me! I already have a component to abstract all the ugly 'OverlayTrigger' code into a neat <Tooltip /> wrapper, so adding this to that was a breeze! I threw a 'disabled' prop onto the <Tooltip />, and used the 'this.props.children' in my abstracted component to replicate the "button" (or whatever else is being wrapped in the tooltip) neatly for the above code!Breeze
thank you, I first tried the solution below this but was getting nasty errors related to properties not have set values. This solution ended up resolving all the problems.Friederike
A
7

This worked for me, shows only when boolean is true.

<OverlayTrigger
  overlay={
    boolean ? (
      <Tooltip id={`tooltip`}>important message for user</Tooltip>
    ) : (
      <span></span>
    )
  }
>
{children}
</OverlayTrigger>
Avis answered 1/2, 2021 at 19:56 Comment(2)
I believe this is preferable to the accepted solution. I'm not getting any errors, and this way you don't have to duplicate or make a special component for your button. Rather, you simply make the ToolTip conditionally render. Nice!Bashaw
thanks Tom Adam I agree with @JB Wilson, your answer is the better. While still not the "accepted" at least I could vote yours to the top. For editing I found it easier to find/replace "overlay={" to "overlay={!boolean ? <span></span> : ". Normally, I try to reserve negative semantics for the "else", but in this case I had many overlays. I'll go back to refactor after I'm confident there are no ill side-effectsGerbold
E
4

The OverlayTrigger component must have a overlay prop passed. If you don't want the tooltip, you also don't want an overlay to trigger. Hence, you'd want to remove it if this.state.show is falsy.

{this.state.show 
  ? <OverlayTrigger overlay={<Tooltip>showing</Tooltip>}>
      <button>Click me!</button>
    </OverlayTrigger>
  : <button>Click me!</button>
}

Edit: Yes, the code in your update would be the way to do it.

Embolic answered 7/9, 2017 at 7:47 Comment(3)
This doesn't help when instead of a simple button you have some content you want to display, but don't have any tooltip for; you'd have to duplicate more than just a button with some text. It should be possible to have an empty overlay with react-bootstrap simply displaying the content without a tooltip. As a workaround, I had to add a class to hide the tooltip in the overlay when I have nothing to show, which is just plain silly.Kiele
This worked perfectly for me! I already have a component to abstract all the ugly 'OverlayTrigger' code into a neat <Tooltip /> wrapper, so adding this to that was a breeze! I threw a 'disabled' prop onto the <Tooltip />, and used the 'this.props.children' in my abstracted component to replicate the "button" (or whatever else is being wrapped in the tooltip) neatly for the above code!Breeze
thank you, I first tried the solution below this but was getting nasty errors related to properties not have set values. This solution ended up resolving all the problems.Friederike

© 2022 - 2024 — McMap. All rights reserved.