How To Add Tooltip in React Application

Introduction

 
In this article, we will learn how to add  React material UI Tooltip in React applications. Tooltip is a pop-up that displays a message or information on hover, click or focus event. Material UI is one of the most popular UI frameworks developed by Google. The Material UI library is designed for faster, easier and developer-friendly user interface development. Now Material-UI is supported by all major browsers and platforms.
 
You can check my previous article in which we discussed ReactJS and its basic components from the below-mentioned links.  
Prerequisites
  • Basic Knowledge of React
  • Visual Studio Code
  • Node and NPM installed
  • Material UI Installed

Create ReactJS project

 
Let's first create a React application using the following command.
  1. npx create-react-app platform  

Install Material-UI

 
Now Install Material-UI by using the following command.
  1. npm install @material-ui/core --save   
Now, right-click on the "src" folder and add a new component named 'Tooltipdemo.js'
 
First let’s add the following reference in this component.
  1. import DeleteIcon from '@material-ui/icons/Delete';    
  2. import Editicon from '@material-ui/icons/Edit';    
  3. import IconButton from '@material-ui/core/IconButton';    
  4. import Tooltip from '@material-ui/core/Tooltip';    
  5. import AppBar from '@material-ui/core/AppBar';    
  6. import Toolbar from '@material-ui/core/Toolbar';   
Add the following code in Tooltipdemo component.
  1. import React, { Component } from 'react'    
  2. import DeleteIcon from '@material-ui/icons/Delete';    
  3. import Editicon from '@material-ui/icons/Edit';    
  4. import IconButton from '@material-ui/core/IconButton';    
  5. import Tooltip from '@material-ui/core/Tooltip';    
  6. import AppBar from '@material-ui/core/AppBar';    
  7. import Toolbar from '@material-ui/core/Toolbar';    
  8. export class Tooltipdemo extends Component {    
  9.         render() {    
  10.                 return (    
  11.                         <div>    
  12.                                 <AppBar position="static">    
  13.                                         <Toolbar style={{ 'paddingLeft'"600px" }}>    
  14.                                                 Material UI Tooltip    
  15.                     </Toolbar>    
  16.                                 </AppBar>    
  17.                                 <div style={{'marginBottom':"20px"}}>    
  18.                                 <Tooltip title="Delete">    
  19.                                         <IconButton aria-label="delete">    
  20.                                                 <DeleteIcon />    
  21.                                         </IconButton>    
  22.                                 </Tooltip>    
  23.                                 <Tooltip title="Edit">    
  24.                                         <IconButton aria-label="Edit">    
  25.                                                 <Editicon />    
  26.                                         </IconButton>    
  27.                                 </Tooltip>    
  28.                                 </div>    
  29.                         </div>    
  30.                 )    
  31.         }    
  32. }   
    export default Tooltipdemo 
Now run the project by using 'npm start' and hover the mouse on the button and check.
 
 

Tooltips

 
The Tooltip has 12 placement positions.
  • RightTop
  • RightCenter
  • RightBottom
  • TopLeft
  • TopCenter
  • TopRight
  • BottomLeft
  • BottomCenter
  • BottomRight
  • LeftTop
  • LeftCenter
  • LeftBottom
We can display toopltip on these positions.
 
Add the following code in the Tooltipdemo component.
  1. import React, { Component } from 'react'    
  2. import DeleteIcon from '@material-ui/icons/Delete';    
  3. import Editicon from '@material-ui/icons/Edit';    
  4. import IconButton from '@material-ui/core/IconButton';    
  5. import Tooltip from '@material-ui/core/Tooltip';    
  6. import AppBar from '@material-ui/core/AppBar';    
  7. import Toolbar from '@material-ui/core/Toolbar';    
  8. import Button from '@material-ui/core/Button';    
  9. export class Tooltipdemo extends Component {    
  10.         render() {    
  11.                return (    
  12.                         <div>    
  13.                                 <AppBar position="static">    
  14.                                         <Toolbar style={{ 'paddingLeft'"600px" }}>    
  15.                                                 Material UI Tooltip    
  16.                                   </Toolbar>    
  17.                                 </AppBar>    
  18.                                 <div style={{ 'marginBottom'"50px" }}>                                          
  19.                                         <Tooltip title="Add" placement="top">    
  20.                                                 <Button>top</Button>    
  21.                                         </Tooltip>    
  22.                                         <Tooltip title="Add" placement="top-end">    
  23.                                                 <Button>top-end</Button>    
  24.                                         </Tooltip>    
  25.                                 </div>    
  26.                         </div>    
  27.                 )    
  28.         }   
  29. }    
  30. export default Tooltipdemo   
    Now open app.js file and the following code in this file
    1. import React from 'react';    
    2. import logo from './logo.svg';    
    3. import './App.css';    
    4. import Tooltipdemo from './Tooltipdemo'    
    5. function App() {    
    6.   return (    
    7.     <div className="App">    
    8.       <Tooltipdemo/>    
    9.     </div>    
    10.   );    
    11. }    
    12. export default App;   
    Run the project and check the result.
     
     

    Summary

     
    In this article, we learned how we add tooltip in React applications using React material UI.