在浏览器中打印 React 组件
# ReactToPrint - 在浏览器中打印 React 组件 打印 React 组件的内容。 `npm install --save React-to-print` ## 演示 ## 使用方法 ```tsx import { useReactToPrint } from "react-to-print"; import { useRef } from "react";
const contentRef = useRef(null); const reactToPrintFn = useReactToPrint({ contentRef });
return (
); ``` 如果要打印的内容是动态的,还可以懒惰设置 ref。详细请参阅 [`LazyContent`](https://github.com/MatthewHerbst/react-to-print/blob/master/examples/LazyContent/index.tsx) 示例。这对于在非 React 代码中设置 ref 也很有用,例如 util 函数。 ## API| 选项 | 类型 | 描述 |
|---|---|---|
bodyClass |
string |
传递给打印窗口的一个或多个类名, 以空格分隔 |
contentRef |
React.RefObject<Element | Text> |
指向要打印的内容的引用。或者, 直接将引用传递给由 useReactToPrint 返回的回调函数 |
copyShadowRoots |
boolean |
将阴影根内容复制到打印窗口。警告: 如果打印大型文档, 请谨慎使用, 因为遍历这些内容可能会很慢。 |
documentTitle |
string | (() => string) |
设置保存为文件时打印的标题。如果使用自定义 print 选项, 此功能有用于包含动态数据, 如时间戳。在传递自定义 print 选项时将被忽略 |
fonts |
{ family: string, source: string; weight?: string; style?: string; }[] |
用于加载到打印 iframe 中的字体列表。如果使用自定义字体, 此功能很有用 |
ignoreGlobalStyles |
boolean |
忽略所有 <style> 和 <link type="stylesheet" /> 标签 |
nonce |
string |
为允许列表脚本和样式元素的内容安全策略 (CSP) 设置 nonce 属性 |
onAfterPrint |
() => void |
在打印对话框关闭后触发的回调函数 |
暂无开放 Issues,或尚未同步最近议题。