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.

Loading a page before the previous one loaded causes setState warning on an unmounted component

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

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
DragomirRhvcommented, Sep 21, 2020

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’;

1reaction
pxFINcommented, Jan 5, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't perform a React state update on an unmounted ...
It work fine. It stops the repetitive call of setState method because it validate _isMounted value before setState call, then at last again ......
Read more >
React state update on an unmounted component - debuggr.io
Warning : Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in...
Read more >
How to work with React the right way to avoid some common ...
Warning : Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your...
Read more >
Avoid React state update warnings on unmounted components
React setState is used to update the state of our components. Normally setState is called during the lifetime of your component.
Read more >
Avoid Memory Leak With React SetState On An Unmounted ...
The “setState warning” exists to help you catch bugs, because calling setState() on an unmounted component is an indication that your app/ ...
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