Blog>
Snippets

Creating Custom Tooltip Components

Provide an example of how to create a custom tooltip component for TanStack React Charts, utilizing the useTooltip hook for displaying additional data on hover.
import { useChart } from 'tanstack-react-charts';
Import the useChart hook from TanStack React Charts to access chart context.
const CustomTooltip = () => {
  const { getTooltipProps } = useChart();
  const tooltipData = getTooltipProps().tooltipData;

  // Conditionally render content based on mouse hover
  if (!tooltipData) return null;

  return (
    <div style={{ padding: '10px', background: '#fff', border: '1px solid #ddd' }}>
      {/* Display data; adjust as needed */}
      <div>Value: {tooltipData.datum?.value}</div>
    </div>
  );
};
Define a CustomTooltip component. Use the useChart hook to access the tooltip's data and conditionally render the tooltip content based on whether data is being hovered over.
export default CustomTooltip;
Export the CustomTooltip for usage in your chart components.