React Tooltip - Customize the Content

Specifying the Content Template

The template implementation depends on the used framework or library. Examples of jQuery, Angular, and ASP.NET MVC are presented below. They show how to create a template consisting of static (text) and dynamic (the Switch UI component) content.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tooltip } from 'devextreme-react/tooltip';
  • import { Switch } from 'devextreme-react/switch';
  •  
  • const renderContent = () => {
  • return (
  • <p>Static content</p>
  • <Switch>
  • {/* The "Switch" UI component is configured here */}
  • </Switch>
  • );
  • }
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <div>
  • <img id="image" src="https://url/to/an/image" />
  • <Tooltip
  • target="#image"
  • showEvent="dxhoverstart"
  • contentRender={renderContent}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;

Switching Templates On the Fly

If you need to render different templates depending on a specific condition, define them inside the Tooltip container using the DevExtreme dxTemplate markup component. You can switch the templates on the fly by changing the contentTemplate property's value.

App.js
styles.css
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Tooltip } from 'devextreme-react/tooltip';
  • import { Button } from 'devextreme-react/button';
  •  
  • const firstTemplate = () => {
  • return (
  • <p>First template</p>
  • );
  • }
  • const secondTemplate = () => {
  • return (
  • <p>Second template</p>
  • );
  • }
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.state = {
  • renderContent: firstTemplate
  • };
  •  
  • this.changeTemplate = this.changeTemplate.bind(this);
  • }
  •  
  • changeTemplate() {
  • this.setState({
  • renderContent: this.state.renderContent === firstTemplate ? secondTemplate : firstTemplate
  • });
  • }
  •  
  • render() {
  • return (
  • <div>
  • <img id="image" src="https://url/to/an/image" />
  • <Tooltip
  • target="#image"
  • showEvent="dxhoverstart"
  • hideEvent="dxhoverend"
  • contentRender={this.state.renderContent}
  • />
  • <Button
  • id="buttonContainer"
  • text="Change the Template"
  • onClick={this.changeTemplate}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;
  • #buttonContainer {
  • display: block;
  • width: 200px
  • }
See Also