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

feat: show previous PDF document while the next one is being created & rendered #91

Merged
merged 1 commit into from
Oct 10, 2022
Merged

feat: show previous PDF document while the next one is being created & rendered #91

merged 1 commit into from
Oct 10, 2022

Conversation

wojtekmaj
Copy link
Contributor

@wojtekmaj wojtekmaj commented Sep 26, 2022

This PR adds a second <Document /> component when PdfViewer detects that the new value is being either still being converted by react-pdf/renderer or being rendered by React-PDF.

I used React-PDF's <Page />'s onRenderSuccess prop to inform PdfViewer that the PDF has been fully rendered. It does this by setting previously rendered PDF blob URL to component's state.

The second <Document /> component holds previous blob URL, while the "main" <Document />, as before, holds the latest value at all times.

By manually managing key props, React prevents updates to whatever component renders previous value, keeping it visible on the screen while the new one is loading.

@vercel
Copy link

vercel bot commented Sep 26, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
site ✅ Ready (Inspect) Visit Preview Sep 26, 2022 at 11:34AM (UTC)

@diegomura
Copy link
Owner

Hi @wojtekmaj ! Tried it and worked perfectly. Thanks for this improvement!

@diegomura diegomura merged commit 2d8592b into diegomura:master Oct 10, 2022
@wojtekmaj wojtekmaj deleted the previous-document branch October 11, 2022 08:05
@ashiqdey
Copy link

How can we make it work with usePDF hook?

const [instance, updateInstance] = usePDF(...);
<Document
  file={instance.url}
  error="Failed"
  renderMode="canvas"
  onLoadSuccess={onLoadSuccess}
>
  {Array(page.total)
    .fill(0)
    .map((_, i) => (
      <Fragment key={i}>
        <Page width={width} pageNumber={i + 1} />
      </Fragment>
    ))}
</Document>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants