11import React , { useEffect , useRef } from 'react' ;
2- import { supportRef } from 'rc-util/lib/ref' ;
2+ import { composeRef , supportRef } from 'rc-util/lib/ref' ;
33import findDOMNode from 'rc-util/lib/Dom/findDOMNode' ;
44import canUseDom from 'rc-util/lib/Dom/canUseDom' ;
55import DomWrapper from './wapper' ;
@@ -16,11 +16,16 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
1616
1717 const wrapperRef = useRef < DomWrapper > ( null ) ;
1818
19+ const elementRef = React . useRef < HTMLElement > ( null ) ;
20+
1921 const canRef = React . isValidElement ( children ) && supportRef ( children ) ;
2022
21- const originRef : React . RefObject < HTMLElement > = canRef
22- ? ( children as any ) ?. ref
23- : null ;
23+ const originRef : React . Ref < Element > = canRef ? ( children as any ) ?. ref : null ;
24+
25+ const mergedRef = React . useMemo < React . Ref < Element > > (
26+ ( ) => composeRef < Element > ( originRef , elementRef ) ,
27+ [ originRef , elementRef ] ,
28+ ) ;
2429
2530 useEffect ( ( ) => {
2631 if ( ! canUseDom ( ) ) {
@@ -30,7 +35,7 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
3035 let instance : MutationObserver ;
3136
3237 const currentElement = findDOMNode (
33- originRef ?. current || wrapperRef ?. current ,
38+ ( originRef as any ) ?. current || wrapperRef ?. current ,
3439 ) ;
3540
3641 if ( currentElement && 'MutationObserver' in window ) {
@@ -53,7 +58,7 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
5358 return (
5459 < DomWrapper ref = { wrapperRef } >
5560 { canRef
56- ? React . cloneElement ( children as any , { ref : ( children as any ) . ref } )
61+ ? React . cloneElement ( children as any , { ref : mergedRef } )
5762 : children }
5863 </ DomWrapper >
5964 ) ;
0 commit comments