Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] getMaxPageCount with useState #69

Open
simon-baehler opened this issue Mar 15, 2021 · 4 comments
Open

[BUG] getMaxPageCount with useState #69

simon-baehler opened this issue Mar 15, 2021 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@simon-baehler
Copy link

Hello,

I'm trying to set the number of pages using useState, but unfortunately, it's crashing.

Describe the bug
I'm trying to use getMaxPageCount with useState

const [pages, setPages] = useState(0);
 <PDFViewer
    externalInput={true}
    document={{
        url: link,
    }}
    canvasCss='customCanvas'
    css='wrapperPDF'
    page={currentPage}
    getMaxPageCount={(pageCount) => setPages(pageCount)}
/>

but I've got this error on the console

react_devtools_backend.js:2430 Error while reading the pages ! TypeError: Cannot read property 'getPage' of null

Expected behavior
the value of pages should best st to the number of pages

What will be awesome is to have a prop like onLoaded that takes a function how to allow us to do some stuff when the pdf was loaded whit success

Have a nice day, thank you for your help

@simon-baehler simon-baehler added the bug Something isn't working label Mar 15, 2021
@ansu5555
Copy link
Owner

You have to call setPages after the PDFViewer component is mounted else it is rendering again while its getting rendered and throwing the error. Use it like below

import React, { useState, useEffect } from 'react'
import PDFViewer from 'pdf-viewer-reactjs'

function App() {
  let pg = 0
  const [pages, setPages] = useState(0)

  useEffect(() => {
    setPages(pg)
  }, [pg])

  return (
    <>
      <PDFViewer
        externalInput={true}
        document={{
          url: link,
        }}
        canvasCss='customCanvas'
        css='wrapperPDF'
        page={currentPage}
        getMaxPageCount={(pageCount) => (pg = pageCount)}
      />

      <h1>{pages}</h1>
    </>
  )
}

export default App

@devkf
Copy link

devkf commented Mar 18, 2021

Thanks for your answer

Not working for me :)

@simon-baehler
Copy link
Author

Same, not working for me ;(

@devkf
Copy link

devkf commented Mar 19, 2021

you can used this https://github.com/MGrin/mgr-pdf-viewer-react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants