question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance Issue causing document to reload, even when file is stored in state

See original GitHub issue

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

I’m not sure if this is a bug or a feature request. This issue was never solved, and I’m encountering it whereby, when I tried to switch pages, the entire document reloads. I even tried it by copying this example exactly (albeit replacing import { Document, Page } from 'react-pdf'; with import {Document, Page} from 'react-pdf/dist/esm/entry.webpack';. I’ve also tried initializing a React state with the file, and passing the state to the file prop with no luck.

Just wanted to know if there is a workaround for this/and or an upcoming fix.

Steps to reproduce

Using the ‘Display single page with navigation’ recipe from the recipes page in the github wiki. Every time I click the next page/prev page button, the entire document reloads.

Expected behavior

Page changing, but document not reloading

Actual behavior

Document is reloading

Additional information

I also tried using pdfjs-dist directly and somehow got it working. I can confirm that it doesn’t reload the document whenever the page changes, so something is wrong here.

Environment

- **Browser (if applicable)**: Chrome 96
- **React-PDF version**: 5.6.0
- **React version**: 17.0.2
- **Webpack version (if applicable)**: using create-react-app

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
egimenoscommented, Dec 16, 2021

I am facing the same exact issue.

Basically, everytime the component rerenders when clicking the next page button, the onLoadSuccess event gets triggered.

Just in case, this is my code. The effect that retrieves the pdf url is only triggered once, I checked since this was my first guess.

import { useState, useEffect, useCallback } from "react";
import { getFileUrl } from "../services/storageService";
import { Document, Page } from "react-pdf/dist/esm/entry.webpack";
import { Text, Box, Button, Flex } from "@chakra-ui/react";

const PDFViewer = ({ fileName }) => {
  const [url, setUrl] = useState(null);
  const [numPages, setNumPages] = useState(null);
  const [pageNumber, setPageNumber] = useState(1);

  const setFileUrl = useCallback(async () => {
    const url = await getFileUrl({ fileName });
    setUrl(url);
  }, [fileName]);

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

  function changePage(offset) {
    setPageNumber((prevPageNumber) => prevPageNumber + offset);
  }

  function previousPage() {
    changePage(-1);
  }

  function nextPage() {
    changePage(1);
  }

  useEffect(() => {
    setFileUrl();
  }, [setFileUrl]);

  const PDF = () => {
    return (
      <Box>
        <Document file={url} onLoadSuccess={onDocumentLoadSuccess}>
          <Page pageNumber={pageNumber} />
        </Document>
        <Flex align="center">
          <Text mr="4">
            Página {pageNumber || (numPages ? 1 : "--")} de {numPages || "--"}
          </Text>
          <Button mr="2" disabled={pageNumber <= 1} onClick={previousPage}>
            Previa
          </Button>
          <Button disabled={pageNumber >= numPages} onClick={nextPage}>
            Siguiente
          </Button>
        </Flex>
      </Box>
    );
  };

  if (!url) return <div>No hay URL</div>;

  return <PDF />;
};

export default PDFViewer;

EDIT I have been reading this comment: https://github.com/wojtekmaj/react-pdf/issues/308#issuecomment-443538284 And also this article in the wiki https://github.com/wojtekmaj/react-pdf/wiki/Frequently-Asked-Questions#react-pdf-reloads-itself-with-every-render-whats-going-on But still can’t see why my Document keeps reloading. I’m saving the url of the file in the state as a string, so unless I’m missing something Document should not detect any change in the file prop.

0reactions
github-actions[bot]commented, Jul 11, 2022

This issue was closed because it has been stalled for 14 days with no activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Performance Problems related to Application ...
NET applications, when a .NET assembly (.NET dll) is loaded into the process address space, it cannot be unloaded. ... So, if for...
Read more >
When does React re-render components? - Felix Gerschau
When the state changes in the parent component (in this case, App ), the two Tile components will re-render, even though the second...
Read more >
Check if page gets reloaded or refreshed in JavaScript
Session values kept until page is closed so it will work only if page reloaded in a new tab with the site. You...
Read more >
Set up React Hot Loader in 10 minutes - LogRocket Blog
A hot reload to an app will only refresh the files that were ... to an app will restart the entire app, and...
Read more >
Hot reload - Flutter documentation
Hot reload works by injecting updated source code files into the running Dart ... to reflect your change, and the current state of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found