File tree Expand file tree Collapse file tree 2 files changed +21
-6
lines changed Expand file tree Collapse file tree 2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change 1
1
import type * as React from 'react' ;
2
- import { isValidElement } from 'react' ;
2
+ import { isValidElement , version } from 'react' ;
3
3
import { ForwardRef , isMemo } from 'react-is' ;
4
4
import useMemo from './hooks/useMemo' ;
5
5
import isFragment from './React/isFragment' ;
6
6
7
+ const ReactMajorVersion = Number ( version . split ( '.' ) [ 0 ] ) ;
8
+
7
9
export const fillRef = < T > ( ref : React . Ref < T > , node : T ) => {
8
10
if ( typeof ref === 'function' ) {
9
11
ref ( node ) ;
@@ -43,10 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => {
43
45
}
44
46
45
47
// React 19 no need `forwardRef` anymore. So just pass if is a React element.
46
- if (
47
- isReactElement ( nodeOrComponent ) &&
48
- ( nodeOrComponent as any ) . props . propertyIsEnumerable ( 'ref' )
49
- ) {
48
+ if ( isReactElement ( nodeOrComponent ) && ReactMajorVersion >= 19 ) {
50
49
return true ;
51
50
}
52
51
Original file line number Diff line number Diff line change 1
1
/* eslint-disable no-eval */
2
2
import React from 'react' ;
3
- import { getNodeRef , useComposeRef } from '../src/ref' ;
3
+ import { getNodeRef , useComposeRef , supportRef } from '../src/ref' ;
4
4
import { render } from '@testing-library/react' ;
5
5
6
6
jest . mock ( 'react' , ( ) => {
@@ -76,4 +76,20 @@ describe('ref: React 19', () => {
76
76
expect ( outerRef . current ?. className ) . toBe ( 'bamboo' ) ;
77
77
expect ( container . querySelector ( '.test-output' ) ?. textContent ) . toBe ( 'bamboo' ) ;
78
78
} ) ;
79
+
80
+ it ( 'supportRef with not provide ref' , ( ) => {
81
+ const Empty = ( ) => < div /> ;
82
+
83
+ const Checker = ( { children } : { children : React . ReactElement } ) => {
84
+ return < p > { String ( supportRef ( children ) ) } </ p > ;
85
+ } ;
86
+
87
+ const { container } = render (
88
+ < Checker >
89
+ < Empty />
90
+ </ Checker > ,
91
+ ) ;
92
+
93
+ expect ( container . querySelector ( 'p' ) ?. textContent ) . toBe ( 'true' ) ;
94
+ } ) ;
79
95
} ) ;
You can’t perform that action at this time.
0 commit comments