Can't perform a React state update on an unmounted component
See original GitHub issueBefore 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
With the following component, I have this warning:
Warning: Can’t perform a React state update on an unmounted component.
Steps to reproduce
This is the problematic component:
class Pdf extends Component {
state = { numPages: 0 }
onDocumentLoad = ({ numPages }) => this.setState({ numPages })
render() {
const { numPages } = this.state
const { url } = this.props
const headers = { withCredentials: true }
return (
<Document file={{ headers, url }} onLoadSuccess={this.onDocumentLoad}>
{range(0, numPages).map(index => (
<Page key={index} pageIndex={index} />
))}
</Document>
)
}
}
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
- React-PDF version:
4.0.2
- React version:
16.8.1
- Webpack version (if applicable):
4.29.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:21 (7 by maintainers)
Top Results From Across the Web
Can't perform a React state update on an unmounted ...
Problem. I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after ...
Read more >React: Prevent state updates on unmounted components
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...
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 your...
Read more >Can't perform a React state update on an unmounted ... - Reddit
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....
Read more >Can't perform a react state update on an unmounted component
To solve the "Warning: Can't perform a React state update on an unmounted component", declare an isMounted boolean in your useEffect hook that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
onLoadSuccess
is called to updatenumberOfPages
.My current solution is to use
useMemo
to memoize the file object. The error disappears completely if I do this.Where
props.fileSrc
is required.Hope this helps.
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.