File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -37,8 +37,11 @@ export default function findDOMNode<T = Element | Text>(
3737 return domNode as T ;
3838 }
3939
40- if ( node && typeof node === 'object' && 'current' in node && isDOM ( node . current ) ) {
41- return node . current as T ;
40+ if ( node && typeof node === 'object' && 'current' in node ) {
41+ const refDomNode = getDOM ( node . current ) ;
42+ if ( refDomNode ) {
43+ return refDomNode as T ;
44+ }
4245 }
4346
4447 return null ;
Original file line number Diff line number Diff line change @@ -76,7 +76,26 @@ describe('findDOMNode', () => {
7676 expect ( true ) . toBeFalsy ( ) ;
7777 }
7878 } ) ;
79+ it ( 'should return DOM node from ref.current' , ( ) => {
80+ const TestComponent = React . forwardRef < HTMLDivElement > ( ( _ , ref ) => {
81+ return < div ref = { ref } > test</ div > ;
82+ } ) ;
83+
84+ const elementRef = React . createRef < HTMLDivElement > ( ) ;
85+ const { container } = render (
86+ < React . StrictMode >
87+ < TestComponent ref = { elementRef } />
88+ </ React . StrictMode > ,
89+ ) ;
90+
91+ expect ( findDOMNode ( elementRef ) ) . toBe ( container . firstChild ) ;
92+ } ) ;
7993
94+ it ( 'should return null if ref is not mounted' , ( ) => {
95+ const elementRef = React . createRef < HTMLDivElement > ( ) ;
96+
97+ expect ( findDOMNode ( elementRef ) ) . toBeNull ( ) ;
98+ } ) ;
8099 it ( 'nativeElement' , ( ) => {
81100 const Element = React . forwardRef < { nativeElement : HTMLDivElement } > (
82101 ( _ , ref ) => {
You can’t perform that action at this time.
0 commit comments