React-pdf: Loading a page before the previous one loaded causes setState warning on an unmounted component

Created on 4 Jan 2019  路  6Comments  路  Source: wojtekmaj/react-pdf

Before you start - checklist

  • [x] I followed instructions in documentation written for my React-PDF version
  • [x] I have checked if this bug is not already reported
  • [x] I have checked if an issue is not listed in Known issues

Description

When rendering one page at a time (first time, no batch loading of pages) state update is done on unmounted component

If renderTextLayer and renderAnnotationLayer is set to false there are no warning messages

Steps to reproduce

In the example code multiple quick clicks to PrevPage/NextPage buttons create warning messages. (if buttons are pressed in slower intervals there are no warning/error messages)

Trace: example.log

If tried similarly on react-pdf test page (load from file (pdf file has to have multiple pages)
http://projekty.wojtekmaj.pl/react-pdf/test

Click next button a couple of times quickly there is a error message

react-pdf-test-site.log

Expected behavior

No error messages

I think the question is though, Is this normal behaviour should application throttle page queries or a bug in react-pdf ?

Also i cant say if this is related but

https://github.com/wojtekmaj/react-pdf/issues/305#issuecomment-447293295

Additional information

Example code:

import React from "react";
import "./App.css";
import "react-pdf/dist/Page/AnnotationLayer.css";
import { Document, Page, pdfjs } from "react-pdf";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${
  pdfjs.version
}/pdf.worker.js`;

class MyPDF extends React.Component {
  constructor(props) {
    super(props);

    this.state = { numPages: null, pageNumber: 1 };
  }

  onDocumentLoadSuccess = ({ numPages }) => {
    this.setState({ numPages });
  };

  goToPrevPage = () => {
    if (this.state.pageNumber > 1) {
      this.setState(state => ({ pageNumber: state.pageNumber - 1 }));
    }
  };
  goToNextPage = () => {
    if (this.state.pageNumber < this.state.numPages) {
      this.setState(state => ({ pageNumber: state.pageNumber + 1 }));
    }
  };

  render() {
    const { pageNumber, numPages } = this.state;

    return (
      <div className="mypdf-container">
        <button onClick={this.goToPrevPage}>Prev</button>
        <button onClick={this.goToNextPage}>Next</button>

        <Document
          file="./example.pdf"
          onLoadSuccess={this.onDocumentLoadSuccess}
        >
          <Page pageNumber={pageNumber} width={600} />
        </Document>

        <p>
          Page {pageNumber} of {numPages}
        </p>
      </div>
    );
  }
}

export default MyPDF;

Environment

  • Browser: Chrome latest, Firefox latest
  • React-PDF version: 4.0.0
  • React version: 16.7
  • Webpack version: create-react-app/react-scripts 2.1.2, webpack 4.19.1
bug

All 6 comments

Hi,
thanks for your report. I see what was wrong - we were setting state even on planned exception thrown by cancelling a promise that was getting text layer items. Fixed - expect it to land with the next release.

Stunning report, BTW :) Took me seconds to understand and reproduce.

馃檹

Thanks, i try to be thorough but not perfectionist ;-)

I can confirm the new release 4.0.2 fixes the problems! Very quick fix, thanks!

Btw, thinking out loud on react-pdf github project page there's a link to Online Demo which goes to your personal webpage.. is this correct or should the link go to the test page ? Also there wasn't a link to the test page on your personal website and i only learned about it from this project github issues

No problem!

Regarding the website, well it should not lead to my personal webpage, but to a project page set up on my personal domain :) I suggest we could move the rest of this discussion to #336 since it suits the topic better :)

I got the same issue now.
Browser: Chrome latest, Firefox latest
React-PDF version: 5.0.0
React version: 16.12.0
Webpack version:

Screenshot at 2020-09-22 01-17-21
4.42.0

It is imported in this way:
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack';
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';

@pxFIN Do you have the same issue now !?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

douglasrcjames picture douglasrcjames  路  4Comments

shivekkhurana picture shivekkhurana  路  4Comments

Crackiii picture Crackiii  路  3Comments

wojtekmaj picture wojtekmaj  路  4Comments

Kerumen picture Kerumen  路  3Comments