Home > Software engineering >  Unable to load pdf in react
Unable to load pdf in react

Time:08-10

I'm trying to load a pdf file for the user in another tab once they click a button but it's not working and I'm not sure how to make it work. Could I have some help in doing this?

I defined a function PdfViewer() and I call it when a button is clicked using onClick(), but once the button is clicked I get this error:

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

Here's my code:

import "../styles/ProjectDetails.css";
import React, { useState } from 'react'
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack'

function PdfViewer() {

    const [numPage, setNumPages] = useState(null);
    const [pageNumber, setPageNumber] = useState(1);

    function onDocumentLoadSuccess({numPages}) {
        setNumPages(numPage);
        setPageNumber(1);
    }

    return (
        <div>
            <header>
                <Document file="../pdfs/Mini_Case_Study_15.pdf" onl oadSuccess={onDocumentLoadSuccess}>
                    <Page height="600" pageNumber={pageNumber}></Page>
                </Document>
            </header>
        </div>
    )
}


const ProjectDetails = ({ project }) => {
    return (
        <div className="card-grid">
            <div className="card">
                <div className="card-header card-image">
                    <img src="https://c4.wallpaperflare.com/wallpaper/672/357/220/road-background-color-hd-wallpaper-thumb.jpg"/>
                </div>
                <div className="card-title"><strong>{project.sdg}</strong></div>
                <div className="card-body">
                    <strong>Goal:</strong> {project.goal}
                </div>
                <div className="card-themes">
                    <strong>Themes:</strong> {project.theme.map((theme)=>{return theme   ', '})}
                </div>
                <div className="card-assignment">
                    <strong>Assignment Type:</strong> {project.assignment_type}
                </div>
                <div className="card-footer">
                    <button className="btn">Details</button>
                    {project.assignment_type === 'Mini Case Studies' &&
                        <>
                            <button className="btn btn-outline">Download</button>
                            {/* <button onClick={PdfViewer} className="btn">Preview</button> */}
                        </>
                    }
                   
                </div>
            </div>
        </div>
        

    )
}

export default ProjectDetails

How do I make it so that once the user clicks the button, it takes them to another page with the pdf file shown?

CodePudding user response:

You could try this approach here, inserting the Preview as a Component.

const ProjectDetails = ({ project }) => {
    const [preview, setPreview] = useState(false)
    
    const onClickToPreviewPDF = () => {
        setPreview(preview ? false : true);
    }
    

    return (
     <>
        <div className="card-grid">
            <div className="card">
                <div className="card-header card-image">
                    <img src="https://c4.wallpaperflare.com/wallpaper/672/357/220/road-background-color-hd-wallpaper-thumb.jpg"/>
                </div>
                <div className="card-title"><strong>{project.sdg}</strong></div>
                <div className="card-body">
                    <strong>Goal:</strong> {project.goal}
                </div>
                <div className="card-themes">
                    <strong>Themes:</strong> {project.theme.map((theme)=>{return theme   ', '})}
                </div>
                <div className="card-assignment">
                    <strong>Assignment Type:</strong> {project.assignment_type}
                </div>
                <div className="card-footer">
                    <button className="btn">Details</button>
                    {project.assignment_type === 'Mini Case Studies' &&
                        <>
                            <button className="btn btn-outline">Download</button>
                            <button onClick={onClickToPreviewPDF} className="btn">Preview</button>
                        </>
                    }
                   
                </div>
            </div>
        </div>
        {preview && <PdfViewer />}
     </>

    )
}
  • Related