简体   繁体   English

SharePoint Framework应用程序中的ReactDom.Render()

[英]ReactDom.Render() in SharePoint Framework App

For our current project, we need to be able to create a PDF file out of the <div></div> s. 对于我们当前的项目,我们需要能够在<div></div>创建PDF文件。

When I look at the code for most of them, they are rending from ReactDom.Render() instead of the render class: 当我查看其中大多数代码时,它们是从ReactDom.Render()而不是render类ReactDom.Render()

An example is from React-pdf : 一个例子来自React-pdf

import React from 'react';
import ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer';

const App = () => (
  <PDFViewer>
    <MyDocument />
  </PDFViewer>
);

ReactDOM.render(<App />, document.getElementById('root'));

I've tried googling and playing around with it, but everything I do throws an error. 我已经尝试使用Google搜索并尝试使用它,但是我所做的一切都会引发错误。

I'm fairly new to SPFx, Javascript, and React. 我对SPFx,Javascript和React相当陌生。

What I'm accustomed to using is the render method: 我习惯使用的是render方法:

export default class PDF extends React.Component<IPDFProps, {}> {
  public render(): React.ReactElement<IPDFProps> {
    return (
        // my components
    );
  }
}

Its not just React-pdf, but pdfMake , react-pdf-js and others. 它不仅是React-pdf,而且是pdfMakereact-pdf-js等。

I can get jsPDF to work, but I don't like how you need to set the coordinates of each part. 我可以使jsPDF正常工作,但是我不喜欢您需要如何设置每个零件的坐标。 If a section changes, you have to redo all the other coordinates. 如果部分更改,则必须重做所有其他坐标。

I think perhaps you are confused as to this line: 我认为您可能对这一行感到困惑:

ReactDOM.render(<App />, document.getElementById('root'));

Basically this is saying render my App component (first arg) and inject it into the dom at this node (second arg). 基本上,这就是说渲染我的App组件(第一个参数),并将其注入到此节点的dom中(第二个参数)。

This is just standard code to inject your react code into the dom. 这只是将您的反应代码注入dom的标准代码。 However, you are free to import and use the PDFViewer whereever you please. 但是,您可以随意导入和使用PDFViewer。 For example, you could do this: 例如,您可以这样做:

  export default class PDF extends React.Component<IPDFProps, {}> {
  public render(): React.ReactElement<IPDFProps> {
    return (
      <PDFViewer>
        <MyDocument />
      </PDFViewer>
    );
  }
}

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

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