简体   繁体   English

如何使用 react-to-print 打印页面

[英]How can I print an page with react-to-print

I need a print button on my form.我的表格上需要一个打印按钮。 I was doing an research for a library to do that, and I found this one:我正在为一个图书馆做一项研究,我发现了这个:

https://www.npmjs.com/package/react-to-print https://www.npmjs.com/package/react-to-print

I was thinking about this flow: a component import and add a line in my code to call that component, and the print button works well, but as I understand it this component needs me to pass the component to be printed in full.我在考虑这个流程:一个组件导入并在我的代码中添加一行来调用该组件,打印按钮效果很好,但据我了解,这个组件需要我传递要完整打印的组件。

I tried to do this, but I got this error:我试图这样做,但我得到了这个错误:

  Attempted import error: './index' does not contain a standard export (imported as 'FormContent').

my index code:我的索引代码:

const App = () => {
    let numb_days = 22
    return (
        <div className="m-4">
            <FormField label="Salário Base" show_small_text="false" numb_days={ numb_days }/>
            <hr />
            <h6 className="mb-4"> Qtd. dias úteis: { numb_days } </h6>
            <FormField label="Auxilio Refeição" show_small_text="true" numb_days={ numb_days }/>
            <FormField label="Auxilio Alimentação" show_small_text="true" numb_days={ numb_days }/>
            <FormField label="Plano de Saúde" show_small_text="false" numb_days={ numb_days }/>
            <FormField label="Outros Benefìcios (VT)" show_small_text="true" numb_days={ numb_days }/>
            <ComponentToPrint ref={(el) => (this.componentRef = el)} />
        </div>
    );
};

my componente code:我的组件代码:

import React, { Component } from "react";
import FormContent from "./index";

class ComponentToPrint extends Component {
  render() {
    return <FormContent ref={(el) => (this.componentRef = el)} />;
  }
}

export default ComponentToPrint;

I think I must be making a big mistake, but I don't understand how I'm going to pass my index on to this component and call my index at the same time.我想我一定是犯了一个大错误,但我不明白我将如何将我的索引传递给这个组件并同时调用我的索引。

I found this example: https://codesandbox.io/s/interesting-cookies-k1bg9?file=/src/deliverySheet/ComponentToPrint.js我找到了这个例子: https://codesandbox.io/s/interesting-cookies-k1bg9?file=/src/deliverySheet/ComponentToPrint.js

it looks like I need to follow the flow:看来我需要遵循流程:

index -> print (my content).

but why couldn't I do that?但为什么我不能这样做? -> ->

index -> my content (print)

or或者

index -> my content AND print

I'm not really sure I understand your question but I'm going to try to answer it, the examples you gave is really hard to read because of all of the subfolders.我不确定我是否理解您的问题,但我会尝试回答,由于所有子文件夹,您提供的示例真的很难阅读。 Basicly what you need to do to print whit react to print is make a normal component and reference it on the same level.基本上,要打印对打印做出反应,您需要做的是制作一个普通组件并在同一级别上引用它。 I see you use class components, so im just going to copy and paste from the react-to-print docs.我看到你使用 class 组件,所以我只是从 react-to-print 文档中复制和粘贴。

import React from 'react';
import ReactToPrint from 'react-to-print';

import { ComponentToPrint } from './ComponentToPrint';

class Example extends React.PureComponent {
  render() {
    return (
      <div>
        <ReactToPrint
          trigger={() => {
            // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop
            // to the root node of the returned component as it will be overwritten.
            return <a href="#">Print this out!</a>;
          }}
          content={() => this.componentRef}
        />
        <ComponentToPrint ref={el => (this.componentRef = el)} />
      </div>
    );
  }
}

Where it says "trigger" is where you render your button.它说“触发器”的地方是你渲染按钮的地方。 I hope that helped.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM