【问题标题】:Render a react component in a new window在新窗口中渲染一个反应组件
【发布时间】:2021-07-21 06:37:15
【问题描述】:

我正在开发一个证书管理系统,在完成所有流程后,用户可以打印证书。

我正在努力实现,以便在单击打印按钮时,将打开一个包含准备打印 HTML 证书的新选项卡,以便用户只需 CTRL + P 即可打印证书。

如何在新窗口中呈现我的 react 证书组件?这样我只会传递要放入证书中的数据的道具,例如姓名,日期等。例如<Certificate name={john} />

我已尝试实现 npm react-new-window,但它不适用于

<Button onclick={() => { 
<NewWindow>
  <CertificateComponent>
</NewWindow>
}}
>
PRINT BUTTON 
</Button>

我已经研究过反应门户,但大多数用例都是针对模态的,这是我的“打印”按钮呈现的地方。

抱歉英语/解释不好。谢谢!

【问题讨论】:

标签: javascript reactjs react-portal


【解决方案1】:
There can be multiple approaches for this,

Approach 1:
create a new route and map the certificateComponent to it, make sure it doesn't have any authentication or any dependency to it.

store the required data for certificateComponent either in session storage or local storage

when the user clicks on print button, redirect the user to this new route using window.open("http://localhost:port/newroute")

in certificateComponent read the values stored in session/local storage and map it accordingly.



Approach2:

Make the certificate component as a overlay which occupies the entire screen which shows up when the user click on print button.
if any ui elements needs to be hided you can do something as shown below,

printFn = function() {
    //show the certificate component 
    // hide the ui elements not required
   // window.print()
}

【讨论】:

    猜你喜欢
    • 2021-01-30
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2017-05-09
    • 2017-02-18
    • 2018-09-02
    相关资源
    最近更新 更多