@@ -78,29 +78,45 @@ export const PDFViewer = forwardRef(function PDFViewer(
7878 useEffect ( ( ) => {
7979 if ( ! containerRef . current ) return ;
8080
81+ // Track if this effect instance is still active (for Strict Mode)
82+ let isActive = true ;
83+
84+ // IMPORTANT: Capture the container reference in a closure
85+ // This ensures we can clean up even after React unmounts the component
86+ const containerElement = containerRef . current ;
87+
8188 // Initialize the viewer with the config prop
8289 const viewer = EmbedPDF . init ( {
8390 type : 'container' ,
84- target : containerRef . current ,
91+ target : containerElement ,
8592 ...config ,
8693 } ) ;
8794
8895 if ( viewer ) {
8996 viewerRef . current = viewer ;
9097 onInit ?.( viewer ) ;
9198
92- // Call onReady when registry is available
99+ // Call onReady when registry is available, but only if still active
93100 if ( onReady ) {
94- viewer . registry . then ( onReady ) ;
101+ viewer . registry . then ( ( registry ) => {
102+ // Only call onReady if this effect instance is still active
103+ // This prevents stale callbacks in React Strict Mode
104+ if ( isActive ) {
105+ onReady ( registry ) ;
106+ }
107+ } ) ;
95108 }
96109 }
97110
98111 return ( ) => {
99- // Cleanup: remove the viewer element
100- if ( viewerRef . current && containerRef . current ) {
101- containerRef . current . innerHTML = '' ;
102- viewerRef . current = null ;
103- }
112+ // Mark this effect instance as inactive
113+ isActive = false ;
114+
115+ // Cleanup: remove the viewer element using captured reference
116+ // We use the captured containerElement instead of containerRef.current
117+ // because React may have already unmounted and cleared the ref
118+ containerElement . innerHTML = '' ;
119+ viewerRef . current = null ;
104120 } ;
105121 // eslint-disable-next-line react-hooks/exhaustive-deps
106122 } , [ ] ) ;
0 commit comments