react-native-pdf: App Crash with this.lastRNBFTask.cancel is not a function

react-native version: 0.72.4

react-native-pdf version: 6.7.4

What platform does your issue occur on? : Both

Describe your issue as precisely as possible :

  1. When the PDF is loading and try to back to the screen that time app crashes
  2. logs TypeError: this.lastRNBFTask.cancel is not a function (it is undefined)

Screenshot Simulator Screenshot - iPhone 15 Pro - 2023-12-23 at 18 09 22

Here is the code: <Pdf trustAllCerts={false} source={{ uri: URL }} onError={(error) => { console.log(error); }} onLoadComplete={(numberOfPages, filePath) => { console.log(‘completed’) }} renderActivityIndicator={() => (<Loader />)} style={style.pdf} />

About this issue

  • Original URL
  • State: open
  • Created 6 months ago
  • Reactions: 8
  • Comments: 28 (1 by maintainers)

Most upvoted comments

I created this patch (patch-package) for avoid this crash:

react-native-pdf+6.7.4.patch

diff --git a/node_modules/react-native-pdf/index.js b/node_modules/react-native-pdf/index.js
index c7c58d8..70a6057 100644
--- a/node_modules/react-native-pdf/index.js
+++ b/node_modules/react-native-pdf/index.js
@@ -127,7 +127,7 @@ export default class Pdf extends Component {
 
         if ((nextSource.uri !== curSource.uri)) {
             // if has download task, then cancel it.
-            if (this.lastRNBFTask) {
+            if (this.lastRNBFTask?.cancel) {
                 this.lastRNBFTask.cancel(err => {
                     this._loadFromSource(this.props.source);
                 });
@@ -145,7 +145,7 @@ export default class Pdf extends Component {
 
     componentWillUnmount() {
         this._mounted = false;
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;
@@ -250,7 +250,7 @@ export default class Pdf extends Component {
 
     _downloadFile = async (source, cacheFile) => {
 
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;

I have downgrade version @6.7.2 its working fine

for me, it lead to a build issue. i used 6.7.3 and it seems fine. no crash

I was having the same issue. reinstalling the package with the latest one solved my crashing issue but then it was not crashing rather it was giving reactnativeblobutil error. by adding trustAllCerts={false} solved the not displaying pdf issue.

  <PDFView
          style={styles.pdfContainer}
          source={{uri: link}}
          onError={onError}
          trustAllCerts={false}
        />

Even-though the patch prevents the app from crashing, it’s not actually fixing the problem, because the cancel is not triggered so the download keeps going in background, which is not ideal.

Downgrading to 6.7.3 seems a better workaround imo, as it actually cancel the download as expected.

For me only @KarlosQ patch is working

I was facing the same issue. Updating to the latest version 6.7.5 has fixed it.

Would you like to create a pull request with those changes?

Hey. Adding “cache: true” to source helped me. Like this: <Pdf source={{ uri: url, cache: true }}

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF.

This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask.

BAD CODE: .catch(async (error) => { this._onError(error); });

comment it out and problem solved.

I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn’t, and the code asks where the hell is my cancel method? Crash.

Well we caught the error before the cancel could do it’s thing…

There is another similar catch on line 323. That one is fine, let it be.

I also have the same issue, when I press back button it arise the same error, then I saw your comment and try to do the same thing as you mentioned. But still it arise error. It didn’t crash the app but arise error on the onError props, but I don’t want to arise any error on the cancel. Then I commented out the below code on the line 146, and it works for me.

// componentWillUnmount() {
//     this._mounted = false;
//     if (this.lastRNBFTask) {
//         this.lastRNBFTask.cancel(err => {
//         });
//         this.lastRNBFTask = null;
//     }
// }