react-to-print is a widely used React component library designed to facilitate printing the contents of React components directly from the browser. It works by creating a temporary iframe, injecting the specified component's DOM, and then triggering the browser's print dialog. The current stable version is 3.3.0, released in February 2026, with frequent patch and minor releases, indicating active maintenance and development. Key differentiators include its `useReactToPrint` hook for functional components, extensive customization options for print styles (like `pageStyle`, `bodyClass`, `ignoreGlobalStyles`), and callbacks for managing print lifecycle events (`onBeforePrint`, `onAfterPrint`, `onPrintError`). It also provides support for loading custom fonts and handling shadow DOMs, addressing common challenges in web printing.
npm install react-to-printVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `useReactToPrint` hook to print a component's content, including ref setup, print options, and lifecycle callbacks.
Migrate class-based `ReactToPrint` usage to the `useReactToPrint` hook for functional components. Ensure `content` option returns the DOM node to be printed.
Use the `pageStyle` option for custom print CSS, or ensure `<link>` and `<style>` tags are properly handled by the browser's print engine. For external stylesheets, ensure they are accessible and loaded within the print iframe. Consider `onBeforePrint` to dynamically inject print-specific styles.
Always check for `componentRef.current` before returning it in the `content` callback, e.g., `content: () => componentRef.current || document.createElement('div')` or ensure the component is mounted before triggering print. The hook's internal logic handles a null return by showing an error.If specific actions need to occur only upon successful print, you might need to implement a more complex tracking mechanism, potentially by combining `onBeforePrint` and a timeout, or accepting the browser's native behavior.
Enable the `copyShadowRoots: true` option if your content includes shadow DOMs. Be aware of potential performance impacts on very large documents and test thoroughly.
Ensure the `ref` (e.g., `componentRef`) is correctly attached to the DOM element you intend to print and that the element is mounted when the print function is invoked. Check the `content` callback always returns a valid DOM element.
Use the `pageStyle` option to inject specific print styles. For dynamic styles, ensure they are rendered into static CSS that can be copied. Check if `ignoreGlobalStyles` is inadvertently set to `true`.
Inspect the `print` iframe (by setting `preserveAfterPrint: true`) to see what content it received. Ensure the component to be printed has visible dimensions and styles that allow it to be rendered in a print-friendly manner. Use `onBeforePrint` to delay printing until content is fully rendered.
If using the class component, ensure it's imported as a default import: `import ReactToPrint from 'react-to-print';`. For new code, prefer the `useReactToPrint` hook: `import { useReactToPrint } from 'react-to-print';`.