import React, { useState, useRef, useEffect } from 'react';
import throttle from 'lodash/throttle';
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack';
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
export default function PDFWrapper({ data, onDocumentLoadSuccess, pageNumber }) {
const [initialWidth, setInitialWidth] = useState(null);
const pdfWrapper = useRef();
const setPdfSize = () => {
setInitialWidth(pdfWrapper.current.getBoundingClientRect().width);
};
useEffect(() => {
window.addEventListener('resize', throttle(setPdfSize, 3000));
setPdfSize();
return () => {
window.removeEventListener('resize', throttle(setPdfSize, 3000));
};
});
return (
<div
id="row"
style={{
height: '100vh', display: 'flex',
}}
>
<div id="placeholderWrapper" style={{ height: '100vh' }} />
<div id="pdfWrapper" style={{ width: '90vw' }} ref={pdfWrapper}>
<Document
file={data}
onLoadSuccess={onDocumentLoadSuccess}
noData=""
loading=""
>
<Page pageNumber={pageNumber} loading="" width={initialWidth} />
</Document>
</div>
</div>
);
}
我将在这里复制整个错误。看起来它来自 setPdfSize 函数。笔记:
TypeError:无法读取 null 的属性“getBoundingClientRect”。10 | 常量 setPdfSize = () => { 11 | setInitialWidth(pdfWrapper.current.getBoundingClientRect().width); | ^ 12 | }; 13 | 14 | useEffect(() => { 任何帮助将不胜感激。