Как использовать библиотеку реагировать-pdf с Typescript - PullRequest
0 голосов
/ 21 февраля 2019

Я пытаюсь использовать библиотеку npm, которая помогает мне рендерить PDF-документ.Я нашел response-pdf, но у него еще нет типов для TypeScript, поэтому я использую его, как показано ниже:

let Document = require('react-pdf');
let Page = require('react-pdf');

class PdfViewer extends React.Component<PdfViewer.Props, PdfViewer.State> {

  constructor(props: PdfViewer.Props) {
    super(props);
    this.state = {
      numPages: null,
      pageNumber: 1,
    };

  }
  onDocumentLoadSuccess = ( numPages: number ) => {
    this.setState({ numPages });

  }

  componentWillReceiveProps(nextProps: PdfViewer.Props) {
    // this.setState(CsvViewer.parse(nextProps.data));
  }

  render() {
      const {url} = this.props;
      const { pageNumber, numPages } = this.state;

      const buffer = data as ArrayBuffer;

      return ( 
      <div>
        <Document
          file={url}
          onLoadSuccess={this.onDocumentLoadSuccess}
        >
          <Page pageNumber={pageNumber} />
        </Document>
        <p>Page {pageNumber} of {numPages}</p>
      </div>
      );

  }
}

Но выдает эту ошибку:

Недопустимый тип элемента: ожидается строка (для встроенных компонентов) или класс / функция (для составных компонентов), но получено: object.

Я нашел this в GitHub, но это мне не сильно помогло.Я перешел к документации библиотеки и передал объект в параметре файла, как они показывают, но с той же ошибкой.Я думаю, что-то еще.Я пошел к исходному коду библиотеки react-pdf и думаю, что это функция, которую они используют для получения файла из параметра:

findDocumentSource = async () => {
    const { file } = this.props;

    if (!file) {
      return null;
    }

    // File is a string
    if (typeof file === 'string') {
      if (isDataURI(file)) {
        const fileUint8Array = dataURItoUint8Array(file);
        return { data: fileUint8Array };
      }

      displayCORSWarning();
      return { url: file };
    }

    // File is PDFDataRangeTransport
    if (file instanceof PDFDataRangeTransport) {
      return { range: file };
    }

    // File is an ArrayBuffer
    if (isArrayBuffer(file)) {
      return { data: file };
    }

    /**
     * The cases below are browser-only.
     * If you're running on a non-browser environment, these cases will be of no use.
     */
    if (isBrowser) {
      // File is a Blob
      if (isBlob(file) || isFile(file)) {
        return { data: await loadFromFile(file) };
      }
    }

    // At this point, file must be an object
    if (typeof file !== 'object') {
      throw new Error('Invalid parameter in file, need either Uint8Array, string or a parameter object');
    }

    if (!file.url && !file.data && !file.range) {
      throw new Error('Invalid parameter object: need either .data, .range or .url');
    }

    // File .url is a string
    if (typeof file.url === 'string') {
      if (isDataURI(file.url)) {
        const { url, ...otherParams } = file;
        const fileUint8Array = dataURItoUint8Array(url);
        return { data: fileUint8Array, ...otherParams };
      }

      displayCORSWarning();
    }

    return file;
  };

Но я сомневаюсь, что ошибка здесь, но я не могувыяснить, что может быть.Я использую версию react 16.8.2 и 4.0.2 для react-pdf

1 Ответ

0 голосов
/ 21 февраля 2019

Это неверно:

let Document = require('react-pdf');
let Page = require('react-pdf');

Вы импортируете модуль весь , когда делаете это.Это означает, что Document и Page оба содержат все модули react-pdf.Вместо этого вам нужно либо импортировать ReactPDF и ссылаться на эти модули:

const ReactPDF = require('react-pdf');

<ReactPDF.Document />
<ReactPDF.Page />

или , вы можете использовать деструктуризацию:

import { Document, Page } from 'react-pdf';

//or

const { Document, Page } = require('react-pdf'); 

Если TypeScript жалуется на отсутствиедекларации, просто добавьте папку @types в свой проект, создайте внутри нее подпапку с именем react-pdf и файл внутри этой папки с именем index.d.ts в одну строку:

declare module "react-pdf"
...