react-popper is the official React wrapper library for Popper.js, a powerful positioning engine used to create tooltips, popovers, and other UI elements that need to be dynamically positioned relative to a reference element. It provides a declarative API for integrating Popper into React applications, abstracting away direct DOM manipulation. The current stable version is 2.3.0, which includes support for React 18. This library is actively maintained, with regular updates addressing bug fixes, TypeScript type improvements, and React version compatibility. Key differentiators include its tight integration with the highly optimized Popper.js core and a modern hook-based API (`usePopper`) introduced in v2.2.0, which simplifies complex positioning logic within functional components, offering a more idiomatic React experience compared to earlier render-prop patterns.
npm install react-popperVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic tooltip implementation using the `usePopper` hook, including placement, offset, and an arrow modifier.
Ensure `@popperjs/core` is installed at version `^2.0.0`. Review any custom Popper options or modifiers for compatibility with Popper v2 API changes, referring to the `@popperjs/core` documentation.
Upgrade to `react-popper` v2.2.1 or newer. Always prefer ESM `import` statements for better tooling compatibility and tree-shaking.
Update to `react-popper` v2.2.4 or later to get the most recent TypeScript type fixes and improved Flow types, ensuring better compatibility with `@popperjs/core`.
Always install both packages: `npm i react-popper @popperjs/core` or `yarn add react-popper @popperjs/core`.
For new implementations or refactoring, prioritize using the `usePopper` hook. Refer to the official documentation for examples.
Ensure the `referenceElement` prop (or the ref setter function in `usePopper`) is correctly assigned to a DOM element that is rendered, or that the element is available when the Popper instance is created. Use `useState` and `useCallback` for refs with `usePopper`.
In the `modifiers` array, for the `arrow` modifier, add `element: arrowElement` where `arrowElement` is a ref to your arrow DOM node (e.g., `<div ref={setArrowElement} />`).Ensure `referenceElement` and `popperElement` states are properly managed and passed to `usePopper`. Also, ensure that the Popper component (or the JSX where styles are applied) is only rendered when `isVisible` (or a similar state) is true, to prevent accessing undefined `styles` or `attributes`.