百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-to-print

> 前端框架
开源

在浏览器中打印 React 组件

2.5K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

在浏览器中打印 React 组件

# ReactToPrint - Print React components in the browser Print the content of a React component. `npm install --save react-to-print` ## Demo ## Usage ```tsx import { useReactToPrint } from "react-to-print"; import { useRef } from "react"; const contentRef = useRef(null); const reactToPrintFn = useReactToPrint({ contentRef }); return ( ); ``` It is also possible to lazy set the ref if your content being printed is dynamic. See the [`LazyContent`](https://github.com/MatthewHerbst/react-to-print/blob/master/examples/LazyContent/index.tsx) example for more. This can also be useful for setting the ref in non-React code, such as util functions. ## API | Option | Type | Description | | :-------------------: | :------- | :---------------------------------------------------------------------------------------------------------------------------------- | | **`bodyClass`** | `string` | One or more class names to pass to the print window, separated by spaces | | **`contentRef`** | `React.RefObject` | The ref pointing to the content to be printed. Alternatively, pass the ref directly to the callback returned by `useReactToPrint` | | **`copyShadowRoots`** | `boolean` | Copy shadow root content into the print window. Warning: Use with care if you print large documents as traversing these can be slow. | | **`documentTitle`** | `string \| (() => string)` | Set the title for printing when saving as a file. This is useful for including dynamic data like timestamps. Ignored when passing a custom `print` option | | **`fonts`** | `{ family: string, source: string; weight?: string; style?: string; }[]` | A list of fonts to load into the printing iframe. This is useful if you are using custom fonts | | **`ignoreGlobalStyles`** | `boolean` | Ignore all `

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Firefox Android (does not support window.print)
  • •Some mobile browser may, instead of printing, open the native Share action instead
  • •onAfterPrint may fire immediately (before the print dialog is closed) on newer versions of Safari where window.print does not block
  • •(401): TypeScript errors such as Type 'undefined' is not assignable to type 'ReactInstance | null'.. You likely need to set your ref to initially be null: useRef(null)

> 标签

TypeScriptjavascriptreactreactjs

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架