Skip to content

RefObject types, and setPage not working in android #985

@leungtinyau27m5

Description

@leungtinyau27m5

for thoes who encountered the setPage not working in android, and the ref object is missing in the type of Pdf, you can use the following patch to fix the problems

diff --git a/android/src/main/java/org/wonday/pdf/PdfView.java b/android/src/main/java/org/wonday/pdf/PdfView.java
index efde3b042bc479c0436d93121afe85277ac968fd..91685076a4207245b9537208d9706c75d1531753 100644
--- a/android/src/main/java/org/wonday/pdf/PdfView.java
+++ b/android/src/main/java/org/wonday/pdf/PdfView.java
@@ -379,6 +379,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
     // page start from 1
     public void setPage(int page) {
         this.page = Math.max(page, 1);
+        this.handlePage(this.page - 1);
     }
 
     public void setEnableRTL(boolean enableRTL) {
diff --git a/index.d.ts b/index.d.ts
index 9463e2c7106750c753ad058ff2b6738cdaf3842e..cfcea725a394ab90f7a156aaca737327b09a4b81 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -65,6 +65,10 @@ export interface PdfProps {
     onPressLink?: (url: string) => void,
 }
 
-declare const Pdf: React.ComponentType<PdfProps> & { setPage(pageNumber: number): void };
+export interface PdfRef {
+    setPage(pageNumber: number): void
+}
+
+declare const Pdf: React.ForwardRefExoticComponent<PdfProps & React.RefAttributes<PdfRef>>
 
 export default Pdf;
import Pdf, { PdfRef } from 'react-native-pdf'

...
const pdfRef = useRef<PdfRef>(null)

const setPage = (page: number) => {
  if (pdfRef && pdfRef.current) pdfRef.current.setPage(page)
}

return (
<Pdf
  ref={pdfRef}
/>
)
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions