React Tooltip Component
react-tooltip is a comprehensive and highly customizable React component for displaying tooltips. Currently at version 5.30.1, it boasts an active development cycle with frequent patch and minor releases that introduce new features, resolve bugs, and enhance performance. Built on `floating-ui`, it offers significant performance improvements over its predecessors and is written entirely in TypeScript, providing robust type definitions out-of-the-box. Key differentiators include extensive customization options via props and data attributes, support for various event triggers (e.g., hover, click), and built-in accessibility features like automatic `aria-describedby` generation. It requires React and ReactDOM versions 16.14.0 or higher as peer dependencies, ensuring compatibility with modern React applications.
Common errors
-
Cannot import the named export 'Tooltip' from 'react-tooltip' (only default export is available)
cause Attempting to use named import syntax for `Tooltip` in a v4 codebase or in a v5 setup where a bundler incorrectly treats the module as CommonJS.fixFor v5+, ensure you are using `import { Tooltip } from 'react-tooltip';`. If the error persists, check your bundler configuration for proper ESM handling (e.g., Webpack's `javascript/auto` for `.mjs` files). For v4, use `import ReactTooltip from 'react-tooltip';`. -
Tooltip is not showing up or not appearing on hover/click.
cause The `id` on the `Tooltip` component does not match the `data-tooltip-id` attribute on the anchor element, or the required styles are not loaded.fixVerify that the `id` prop on `<Tooltip>` exactly matches the `data-tooltip-id` attribute on your interactive element. Also, ensure that `react-tooltip`'s default styles are being imported (e.g., `import 'react-tooltip/dist/react-tooltip.css';`). -
Tooltip content is not updating dynamically after state changes.
cause In v4, users expected to call `ReactTooltip.rebuild()`. In v5, while automatic, complex DOM manipulations or certain rendering patterns might cause issues if the component doesn't detect changes.fixEnsure you are on v5+ (which handles updates automatically). If on v4, call `ReactTooltip.rebuild()` after dynamic content changes. For v5, if automatic updates fail, ensure your anchor element is correctly rendered and its `data-tooltip-content` or `content` prop is bound to reactive state. Consider using the `setIsOpen` prop for explicit control in complex scenarios. -
Type 'Element' is not assignable to type 'ReactElement<any, any> | null' in TypeScript with `TooltipRef`.
cause Incorrectly assigning a non-ReactElement type to a ref that expects a React element or component instance for `TooltipRef`.fixEnsure that the `ref` prop is correctly attached to the `Tooltip` component instance, e.g., `const tooltipRef = useRef<TooltipRef>(null); <Tooltip ref={tooltipRef} ... />`. Avoid manually trying to set the ref to a DOM element directly when the type expects a component ref.
Warnings
- breaking Major breaking changes occurred in v5. All data attributes are now prefixed with `data-tooltip-`. For example, `data-for` became `data-tooltip-id` and `data-tip` became `data-tooltip-content`.
- breaking The main component export changed from a default export named `ReactTooltip` in v4 to a named export `Tooltip` in v5.
- breaking The `ReactTooltip.rebuild()` method, used in v4 for dynamic content updates, was removed in v5. Tooltips now automatically watch for DOM changes.
- breaking The `getContent` prop was removed in v5, and content is primarily managed via `data-tooltip-content` or the `content` prop on the `Tooltip` component.
- gotcha The default behavior of the tooltip's effect changed from `float` to `solid` in v5.
- gotcha Bundling issues can occur in some environments (e.g., SPFx) where `react-tooltip`'s ESM module is incorrectly treated as CommonJS, leading to 'Can't import named export from non EcmaScript module' errors.
Install
-
npm install react-tooltip -
yarn add react-tooltip -
pnpm add react-tooltip
Imports
- Tooltip
import ReactTooltip from 'react-tooltip';
import { Tooltip } from 'react-tooltip'; - ITooltipController
import type { ITooltipController } from 'react-tooltip'; - TooltipRef
import type { TooltipRef } from 'react-tooltip';
Quickstart
import React, { useRef, useState } from 'react';
import { Tooltip, TooltipRef } from 'react-tooltip';
function MyComponent() {
const tooltipRef = useRef<TooltipRef>(null);
const [isTooltipOpen, setIsTooltipOpen] = useState(false);
const showProgrammaticTooltip = () => {
if (tooltipRef.current) {
tooltipRef.current.showTooltip();
setIsTooltipOpen(true);
}
};
const hideProgrammaticTooltip = () => {
if (tooltipRef.current) {
tooltipRef.current.hideTooltip();
setIsTooltipOpen(false);
}
};
return (
<div>
<a
data-tooltip-id="my-hover-tooltip"
data-tooltip-content="Hello, hover world!"
>
Hover over me
</a>
<Tooltip id="my-hover-tooltip" />
<br /><br />
<button
data-tooltip-id="my-click-tooltip"
data-tooltip-content="Click me for content!"
>
Click for Tooltip
</button>
<Tooltip id="my-click-tooltip" place="right" events={['click']} />
<br /><br />
<button onClick={showProgrammaticTooltip} disabled={isTooltipOpen}>
Show Programmatic Tooltip
</button>
<button onClick={hideProgrammaticTooltip} disabled={!isTooltipOpen}>
Hide Programmatic Tooltip
</button>
<span
id="programmatic-anchor"
style={{ marginLeft: '10px', padding: '5px', border: '1px solid gray' }}
>
Anchor
</span>
<Tooltip
id="programmatic-tooltip"
content="I'm controlled by buttons!"
isOpen={isTooltipOpen}
setIsOpen={setIsTooltipOpen}
anchorSelect="#programmatic-anchor"
ref={tooltipRef}
place="bottom"
/>
</div>
);
}
export default MyComponent;