If I understand the question (and comment dialogue) correctly, you want to
- create multiple tooltip elements
- have some sort of
toolTipMsg()
function to read the content of the shown tooltip
The current setup looks like a long roundtrip:
- inside react render function, use jQuery to create multiple tooltip elements, and pass each a HTML data-name attribute
- inside react componentDidMount, use jQuery to fetch tooltips from DOM and attach listeners
- listener fires function that reads again from DOM to fetch tooltip HTML data-name attribute.
Mixing jQuery and react for reading DOM (or - worse - manipulating DOM) is generally not a good idea.
UPDATE: so there are 2 possible routes to solve:
a. React-only solution (NO jQuery, NO Bootstrap)
Within react it is good practice to avoid using refs if they are not needed. And it looks like you do not need them.
You could bind the variable to the toolTipMsg()
, and use that.
And set up a MouseOver listener on the parent of the tooltip.
React-style code would look more or less like:
// tooltip element creation inside render function
// of individual tooltip parent element
return
<div className="alwaysVisibleToolTipParent"
onMouseOver={this.toolTipMsg}>
<p>someOtherContent</p>
<span className="my-awesome-tooltip">{this.props.tooltipname}</span>
</div>
});
// no need for componentDidMount: listener covered in initial render
// the function is much simpler now
tooltipMsg: function(){
console.log(this.tooltipname);
}
You need to roll your own tooltip with styling for this (so unfortunately no benefit from jQuery)
b. Alternatively: Use Bootstrap and jQuery only (NO react)
Bootstrap tooltip fires its own events that you can listen to. (documentation here).
In your code, you would somewhere:
$('#myToolTipParent').on('shown.bs.tooltip', function () {
// 'this' is now the element (parent of the tooltip)
tooltipMsg(this);
})
And your tooltipMsg()
function would read:
function tooltipMsg(element) {
// the text of the tooltip == the title attribute of the parent element of the tooltip
console.log(element.getAttribute("title"));
}