-
-
Notifications
You must be signed in to change notification settings - Fork 848
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
Can't perform a React state update on an unmounted component #353
Comments
Hello @Kerumen , try this: onDocumentLoad = (document) => {
const { numPages } = document;
this.setState({
numPages,
});
}; see the example here |
@Kerumen did you get it working ? @Maxhou00 I tried the approach mentioned here but facing same issue as mentioned above it keeps on loading and keep making Network calls. "Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method. |
@qaisershehzad Perhaps you're defining new |
@wojtekmaj I am doing this : |
@wojtekmaj I am able to get it work by doing like this : |
I also had the same "React state update on an unmounted component" message. However, this seemed to happen when I had a multiple page PDF open in react-pdf, and was shifting from one page to another before the first page was loaded. My "fix" was to disable all controls until the "onRenderSuccess" event was fired on a page load. Try to load a new page: disable all controls, wait for "onRenderSuccess". However, I don't know if this is overkill. @wojtekmaj : does this sound like the right thing to do? |
We should be able to cancel setting state on PageInternal unmount, so this sounds like a bug that would be relatively easy to tackle. I don't think disabling all controls is worth it - the bug itself won't cause anything to crash - but it's a workaround that should do it if you really don't like seeing this message for now :) |
Thank you for your comment. I'd look forward to that bug being fixed. |
Hi @peterkmurphy, I am having the same issue but am not sure what you mean by disabling controls until onRenderSuccess gets called. I am doing this:
Can you please elaborate on your solution? Thank you :) |
Would also be interested in hearing the solution to this. Error is appearing in a component using React Hooks, so I'm not sure how that plays into the mix. |
I ran into this issue while using this library in a next.js project. My understanding is that the error happens during rerenders - in my case, when My current solution is to use
Where Hope this helps. |
@qaisershehzad I've been struggling through it almost 2 days. Your approach with having the whole file object in state finally helped me. Thanks |
Potentially fixes a common issue described #353 caused by creating objects in render method, but may introduce some behavioral differences e.g. refusing to load new PDF file with the same size.
Memoizing objects / keeping them in state is the correct, recommended solution. I'll make sure to make better documentation of that matter until we have some better solution (like the commit I just referenced above - work in progress, but "seems to work fine"). |
It will cancel request when unmount the component. The promise reject then. Line 77 in 5900778
In Line 264 in 5900778
But I think the error should be classified to determine it's a network error or cancelled tasks. If it's cancelled by unmount life cycle method, it should not setState or do anything else.
|
Facing the same error as well, when attempting to update numPages on a mounted component. Already using file in state - the issue is only when using onDocumentLoadSuccess: |
Hmm, reproduced your onDocumentLoadSuccess and I can't see the error @scottie-schneider :( Wanna take a look? |
Hi, |
@wojtekmaj |
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days. |
This issue was closed because it has been stalled for 14 days with no activity. |
Before you start - checklist
Description
With the following component, I have this warning:
Steps to reproduce
This is the problematic component:
Expected behavior
If I remove the
setState
, the PDF renders properly but I lose the information about the number of pages.Additional information
This is the example from the README and this was perfectly working before I upgraded to v4.
Environment
4.0.2
16.8.1
4.29.3
The text was updated successfully, but these errors were encountered: