Reputation: 17
I'tm trying to implement a pdf viewer using pdfjs-dist and react with bootstrap. It's very simple, but every time I try to run the app and upload a pdf, I get the error "caught TypeError: Cannot read properties of undefined (reading 'current')", and I can't really find the error in the code. My version of worker-url is 1.1.0 and my version of pdfs-dist is 2.16.105. I have been following a youtube tutorial (https://www.youtube.com/watch?v=9eMFU3oV7cQ) but my problem doesn't seem to appear there and I cant find why I get the error. The code I have is the following
import React, { useState } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.bundle.min.js';
import 'pdfjs-dist/web/pdf_viewer.css';
import 'pdfjs-dist/build/pdf.worker.min';
import './App.css';
import {Viewer, Worker} from '@react-pdf-viewer/core'
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout'
import '@react-pdf-viewer/core/lib/styles/index.css'
import '@react-pdf-viewer/default-layout/lib/styles/index.css'
function App() {
const defaultLayoutPluginInstance = defaultLayoutPlugin(
);
const [pdfFile, setPDFFile] = useState(null)
const [viewPDF, setViewPDF] = useState(null)
const fileType = ['application/pdf']
const handleChange = (e) => {
let selectedFile = e.target.files[0]
if(selectedFile) {
if(selectedFile && fileType.includes(selectedFile.type)) {
let reader = new FileReader()
reader.readAsDataURL(selectedFile)
reader.onload = (e) => {
setPDFFile(e.target.result)
}
}
else {
setPDFFile(null)
}
}
else {
console.log("please select")
}
}
const handleSubmit = (e) => {
e.preventDefault()
if(pdfFile !== null) {
setViewPDF(pdfFile)
}
else {
setViewPDF(null)
}
}
return (
<div className="container-fluid h-100">
<div className="row h-100">
<div className="col-3 px-1 bg-light position-fixed" id="sticky-sidebar">
<div className="accordion accordion-flush" id="accordionFlushExample">
<div className="accordion" id="accordionExample">
<div className="accordion-item">
<h2 className="accordion-header">
<button className="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" className="accordion-collapse collapse show" data-bs-parent="#accordionExample">
<div className="accordion-body">
<strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div className="accordion-item">
<h2 className="accordion-header">
<button className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" className="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div className="accordion-body">
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div className="accordion-item">
<h2 className="accordion-header">
<button className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div id="collapseThree" className="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div className="accordion-body">
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div className="accordion-item">
<h2 className="accordion-header">
<button className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
Accordion Item #4
</button>
</h2>
<div id="collapseFour" className="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div className="accordion-body">
<strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
</div>
</div>
</div>
<div className={`col-9 offset-3 h-100 main-area`} id="main">
<form onSubmit={handleSubmit}>
<input type="file" className='form-control' onChange={handleChange}/>
<button type='submit' className='btn btn-success'>View PDF</button>
</form>
<h2>View PDF</h2>
<div className='pdf-container'>
<Worker workerUrl="https://unpkg.com/[email protected]/build/pdf.worker.min.js">
{viewPDF && <>
<Viewer fileUrl={viewPDF} plugins={[defaultLayoutPluginInstance]}/>
</>}
{!viewPDF && <> No PDF</>}
</Worker>
</div>
</div>
</div>
</div>
);
}
export default App;
Thank you for your time to anyone who tries to help me.
Upvotes: 0
Views: 1586
Reputation: 17
I solved it by changing the version of @react-pdf-viewer/core": "^3.12.0 and @react-pdf-viewer/default-layout": "^3.12.0 to the same version, since one of them was using 3.7.0. Thank you for your time!
Upvotes: 0