Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do use bootstrap tooltips with React?

I had tooltips working earlier and am trying to migrate my component to React. I'm not using react-bootstrap yet because I'm not sure if I'm going to because it's still under heavy development and not 1.0 yet.

Here's a snippet of what my render code looks like:

<span>
    <input data-toggle="tooltip" ref="test" title={this.props.tooltip}  type="radio" name="rGroup" id={"r" + this.props.name} />
    <label className="btn btn-default" htmlFor={"r" + this.props.name}></label>
</span>

And calling it:

<MyComponent name="apple" tooltip="banana" />

I know you have to call the tooltip function to get it to show up and I think that's where I'm messing up. I'm currently trying something like this:

componentDidMount() {
    $(this.refs.test).tooltip();
    // this.refs.test.tooltip(); ?
    // $('[data-toggle="tooltip"]').tooltip(); ?
}

But none of this seems to be working. The tooltip isn't showing up.

like image 515
Ryan Peschel Avatar asked Nov 11 '15 17:11

Ryan Peschel


People also ask

How do I use Bootstrap tooltip in React?

We can use the following approach in ReactJS to use the react-bootstrap Tooltip Component. Tooltip Props: arrowProps: It is used to position the tooltip arrow. id: It is just an HTML id attribute that is necessary for accessibility.

How do you use tooltip in React?

We can use the Tooltip Component in ReactJS using the following approach. Creating React Application And Installing Module: Step 1: Create a React application using the following command. Step 2: After creating your project folder i.e. foldername, move to it using the following command.

How do you implement Bootstrap tooltip?

How To Create a Tooltip. To create a tooltip, add the data-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with jQuery: select the specified element and call the tooltip() method.

Can you use Bootstrap and React together?

Bootstrap can be used directly on elements and components in your React app by applying the built-in classes as you would any other class.


3 Answers

It would be better if you use real React components because React only writes to the DOM so it cannot recognize any changes made my others which could clash with.

https://github.com/react-bootstrap/react-bootstrap

const tooltip = (
  <Tooltip id="tooltip">
    <strong>Holy guacamole!</strong> Check this info.
  </Tooltip>
);
const overlay = (
    <OverlayTrigger placement="left" overlay={tooltip}>
      <Button bsStyle="default">Holy guacamole!</Button>
    </OverlayTrigger>
);

(Example taken from https://react-bootstrap.github.io/components/tooltips/)

like image 123
shaedrich Avatar answered Sep 21 '22 02:09

shaedrich


Without using refs / React DOM, you can select tooltips by data-toggle attribute:

componentDidMount() {
  $('[data-toggle="tooltip"]').tooltip();
}

componentDidUpdate() {
  $('[data-toggle="tooltip"]').tooltip();
}

Source: Bootstrap documentation

Note, if you're loading jQuery by CDN, you can refer to it by window.$ as well.

like image 21
devonj Avatar answered Oct 16 '22 18:10

devonj


I know it s an old question, but to avoid side effects with bootstrap/jquery/react, if you want tooltips, use this :

https://www.npmjs.com/package/react-tooltip

It's very easy to use and works better than bootstrap tooltip in a react project

like image 8
Guillaume Harari Avatar answered Oct 16 '22 18:10

Guillaume Harari