@@ -3,6 +3,7 @@ import React, {
33 ReactNode ,
44 RefObject ,
55 forwardRef ,
6+ useCallback ,
67 useEffect ,
78 useImperativeHandle ,
89 useRef ,
@@ -60,9 +61,9 @@ function Frame({
6061 const isMounted = useRef ( false ) ;
6162 const loadCheckInterval = useRef < ReturnType < typeof setInterval > | null > ( null ) ;
6263
63- const getDoc = ( ) : Document | null => {
64+ const getDoc = useCallback ( ( ) : Document | null => {
6465 return nodeRef . current ? nodeRef . current . contentDocument : null ;
65- } ;
66+ } , [ nodeRef ] ) ;
6667
6768 const getMountTarget = ( ) : Element | null => {
6869 const doc = getDoc ( ) ;
@@ -74,42 +75,40 @@ function Frame({
7475 return doc . body . children [ 0 ] ;
7576 } ;
7677
77- const handleLoad = ( ) => {
78+ const handleLoad = useCallback ( ( ) => {
7879 if ( loadCheckInterval . current ) {
7980 clearInterval ( loadCheckInterval . current ) ;
8081 }
8182 if ( ! iframeLoaded ) {
8283 setIframeLoaded ( true ) ;
8384 }
84- } ;
85+ } , [ iframeLoaded ] ) ;
8586
8687 useEffect ( ( ) => {
8788 isMounted . current = true ;
8889
8990 const doc = getDoc ( ) ;
91+ const frame = nodeRef . current ;
92+ const interval = loadCheckInterval . current ;
9093
91- if ( doc && nodeRef . current ?. contentWindow ) {
92- nodeRef . current . contentWindow . addEventListener (
93- 'DOMContentLoaded' ,
94- handleLoad
95- ) ;
94+ if ( doc && frame ?. contentWindow ) {
95+ frame . contentWindow . addEventListener ( 'DOMContentLoaded' , handleLoad ) ;
9696 }
9797
9898 return ( ) => {
9999 isMounted . current = false ;
100100
101- if ( nodeRef . current ?. contentWindow ) {
102- nodeRef . current . contentWindow . removeEventListener (
103- 'DOMContentLoaded' ,
104- handleLoad
105- ) ;
101+ if ( frame ?. contentWindow ) {
102+ frame . contentWindow . removeEventListener ( 'DOMContentLoaded' , handleLoad ) ;
106103 }
107104
108- if ( loadCheckInterval . current ) {
109- clearInterval ( loadCheckInterval . current ) ;
105+ if ( interval ) {
106+ clearInterval ( interval ) ;
110107 }
111108 } ;
112- } , [ ] ) ;
109+ // nodeRef is stable (either internalRef from useRef or external ref from props)
110+ // eslint-disable-next-line react-hooks/exhaustive-deps
111+ } , [ getDoc , handleLoad ] ) ;
113112
114113 const renderFrameContents = ( ) : ReactNode => {
115114 if ( ! isMounted . current ) {
0 commit comments