react-to-print
react-to-print
Print React components in the browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import React, { useRef } from 'react';
import ReactToPrint from 'react-to-print';
// 定义要打印的组件
const ComponentToPrint = () => {
return (
<div>
<h1>这是要打印的内容</h1>
<p>这里可以包含更多详细信息。</p>
</div>
);
};
const PrintButton = () => {
const componentRef = useRef();
return (
<div>
{/* 渲染要打印的组件 */}
<ComponentToPrint ref={componentRef} />
{/* 创建打印按钮 */}
<ReactToPrint
trigger={() => <button>打印此内容</button>}
content={() => componentRef.current}
/>
</div>
);
};
export default PrintButton;
|